HDU1075 What Are You Talking About

题目链接:HDU1075

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 18936    Accepted Submission(s): 6202


Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 

Output
In this problem, you have to output the translation of the history book.
 

Sample Input
   
   
   
   
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 

Sample Output
   
   
   
   
hello, i'm from mars. i like earth!
Hint
Huge input, scanf is recommended.

题意:只有一组数据输入,第一次start与end中间的是英文与火星文的对照表,下一个start与end中间的是原文,我们要把它翻译成人类能读懂的语言。

题目分析:这题属于字典树的练习题,主要是注意非火星文的部分要原样输出出来,还有就是当有的火星文是其他火星文前缀的时候要注意判段,我们在每个节点加一个flag变量判断是不是一个串的结尾就好了。

//
//  main.cpp
//  HDU1075
//
//  Created by teddywang on 16/3/26.
//  Copyright © 2016年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef struct node{
    int num;
    int flag;
    node *p[26];
}trienode;
trienode *root;
char s[1000000][12];

void insert_node(char *s,int num)
{
    int len=strlen(s);
    trienode *h,*t=root;
    for(int i=0;i<len;i++)
    {
         int pos=s[i]-'a';
        if(t->p[pos]==NULL)
        {
            h=new node;
            for(int i=0;i<26;i++)
                h->p[i]=NULL;
            h->num=h->flag=0;
            t->p[pos]=h;
        }
        t=t->p[pos];
        if(i==len-1)
        {
            t->flag=1;
            t->num=num;
        }
    }
}

int find(char *s)
{
    trienode *t=root;
    int num=0;
    int len=strlen(s);
    for(int i=0;i<len;i++)
    {
        int buf=s[i]-'a';
        if(t->p[buf]==NULL)
        {
            return 0;
        }
        else
        {
            t=t->p[buf];
            num=t->num;
        }
    }
    if(t->flag==1)
    {
        num=t->num;
        return num;
    }
    
    else return 0;
}

int main()
{
    root=new node;
    for(int i=0;i<26;i++)
    {
        root->p[i]=NULL;
    }
    root->num=0;root->flag=0;
    char f[4000],t[4000];
    int j=0;
    scanf("%s",s[j++]);
    while(1)
    {
        scanf("%s",s[j]);
        if(s[j][0]=='E') break;
        j++;
        scanf("%s",s[j]);
        insert_node(s[j],j-1);
        j++;
    }
    char b[10];
    getchar();
    gets(b);
    while(1)
    {
        gets(t);
        if(t[0]=='E') break;
        int len=strlen(t);
        int k=0;
        for(int i=0;i<len;i++)
        {
            if(t[i]>='a'&&t[i]<='z')
            {
                f[k++]=t[i];
            }
            else if(k>0)
            {
                int buf=find(f);
                if(buf>0) printf("%s",s[buf]);
                else printf("%s",f);
                memset(f,0,sizeof(f));
                k=0;
            }
            if(k==0)
            {
                printf("%c",t[i]);
            }
        }
        printf("\n");
    }
}


你可能感兴趣的:(字典树)