What Are You Talking About HDU - 1075(字典树)

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<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N=7e6+1;
const int mod=1e9+7;
const double pi=acos(-1);
const double eps=1e-8;
int trie[N][26],tot;
bool vis[N];
char a[26],b[26],s[N],ans[N][26];//ans数组存翻译的单词
string t;
void sert()
{
    int root=0;
    for(int i=0;b[i]!='\0';i++)
    {
        int id=b[i]-'a';
        if(!trie[root][id]) trie[root][id]=++tot;
        root=trie[root][id];
    }
    vis[root]=true;
    strcpy(ans[root],a);//将字符串a存到ans数组里
}
int finf()
{
    int root=0;
    for(int i=0;t[i]!='\0';i++)
    {
        int id=t[i]-'a';
        if(!trie[root][id]) return 0;
        root=trie[root][id];
    }
    if(vis[root]) return root;
    return 0;
}
int main()
{
    scanf("%s",a);
    while(~scanf("%s",a)&&a[0]!='E')
    {
        scanf("%s",b);
        sert();
    }
    scanf("%s",a);
    getchar();//注意吸收换行,不然PE
    while(gets(s))
    {
        if(s[0]=='E') break;
        t="";
        for(int i=0;s[i]!='\0';i++)
        {
            if(s[i]>='a'&&s[i]<='z')
                t+=s[i];
            else
            {
                if(finf()) printf("%s",ans[finf()]);
                else cout<<t;
                t="";
                printf("%c",s[i]);
            }
        }
        printf("\n");
    }
}

猜你喜欢

转载自blog.csdn.net/never__give__up/article/details/80318113
今日推荐