您的位置:首页 > 其它

hdu 1075 What Are You Talking About (字典树·文字翻译)

2015-08-12 08:52 375 查看
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1075

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.


大意:给出字典类内容和需要翻译的文章输出翻译后的文字内容。

字典树应用。在字典树上每一个单词结尾字符的节点保存英语单词信息和结束信息,然后就是字符串的处理了。

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node{
    bool cover;
    char word[15];
    node *next[26];
    node(){
        cover=0;
        memset(next,0,sizeof(next));
        memset(word,0,sizeof(word));
    }
};
node *root;
void insert(char *s1,char *s2){
     int length=strlen(s2);
     node *t=root;
     for(int i=0;i<length;i++){
         if(t->next[s2[i]-'a']==0){
              t->next[s2[i]-'a']=new node(); //cool
         }
         t=t->next[s2[i]-'a'];
     }
     t->cover=1;
     strcpy(t->word,s1);
}
void del(node *p){
    if(p==NULL)return ;
    for(int i=0;i<26;i++)del(p->next[i]);
    delete p;
}
char* query(char *s){
    node *t=root;
    int length=strlen(s);
    for(int i=0;i<length;i++){
         if(t->next[s[i]-'a']==0)return s;
         t=t->next[s[i]-'a'];
    }
    if(t->cover)return t->word;  //cover=1的作用:代表是完整的词
    return s;
}
char str[3005],s1[15],s2[15];
int main(){
    //freopen("cin.txt","r",stdin);
    root=new node ();
    cin>>str;
    while(~(scanf("%s",s1))){
        if(strcmp(s1,"END")==0)break;
        scanf("%s",s2);
        insert(s1,s2);
    }
    cin>>str;
    getchar();  //gets 也吸收'\n' 连续使用却不会
    while(gets(str)){
        if(str[0]=='E')break;  //不知道为何我用if(strcmp(str,"END")==0)break; 就不能跳出,输出str是"END"的啊
        char ss[20];
        int length=strlen(str),top=0;
        str[length++]='\n';
        for(int i=0;i<length;i++){
            if(!(str[i]>=97&&str[i]<=122)){
                ss[top]=0;
                if(top>0)printf("%s",query(ss));
                printf("%c",str[i]);
                top=0;
            }
            else {  //小写字母
                ss[top++]=str[i];
            }
        }
    }
    del(root);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: