http://begin.lydsy.com/JudgeOnline/problem.php?id=2770(PKU2503 Babelfish)

2770: PKU2503 Babelfish

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 2  Solved: 2
[Submit][Status][Web Board]

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect
of a foreign language. Fortunately, you have a dictionary to help you understand them.
我们人类的单词在老鼠的词典里面并不适用,所以我们必须来翻译这些词语。这里有许多组人类的单词在老鼠词典里面的写法是什么样的,之后我们有许多询问,对于每个询问求出每个询问在字典里面对应的单词是什么。

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by
a message of up to 100,000 words. Each dictionary entry is a line containing an English word
followed by a space and a foreign language word. No foreign word appears more than once in the dictionary.
The message is a sequence of words in the foreign language, one word on each line. Each word in the input
is a sequence of at most 10 lowercase letters.

Output

Output is the message translated to English, one word per line. Foreign words not
in the dictionary should be translated as "eh".

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay

atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

HINT

 

Source

 题解:
  这个看一眼就知道是trie树,随便乱搞搞就可以了。。。。。。
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 //#include<string>
 5 #define maxn 10000
 6 using namespace std;
 7 int sum[350000];
 8 int son[350000][26];
 9 char s1[100005][12],s2[100005][12];
10 char s[100005];
11 int tot,i,j;
12 void insert(char *s,int num)
13 {
14     int p=0;
15     for (int i=0; s[i]; p=son[p][s[i]-'a'],i++)
16         if (!son[p][s[i]-'a']) son[p][s[i]-'a']=++tot;
17     sum[p]=num;
18 }
19 int answer(char *s)
20 {
21     int p=0;
22     for (int i=0; s[i]; p=son[p][s[i]-'a'],i++)
23             if (!son[p][s[i]-'a'] ) return 0;
24     return sum[p];
25 } 
26 int main()
27 {
28     int i=1;
29     while (true) 
30     {
31         gets(s);
32         if (s[0]=='') break;
33         sscanf(s,"%s %s",s1[i],s2[i]);
34         insert(s2[i],i);
35         i++;
36     }
37     while (scanf("%s",s)!=-1)
38     {
39         i=answer(s);
40         if (i==0) cout<<"eh"<<endl; else  cout<<s1[i]<<endl;
41     }
42 }
View Code
原文地址:https://www.cnblogs.com/HQHQ/p/5367963.html