POJ 2503 Babelfish
Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 42986 | Accepted: 18199 |
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
Huge input and output,scanf and printf are recommended.
Source
Waterloo local 2001.09.22
//字典树:
#include <iostream>
#include <iomanip>
#include <cstring>
#include <map>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Trie
{
int cnt;
bool flag;
char s[45];//保存的字符串
struct Trie *next[26];
Trie():cnt(0),flag(false){memset(next,0,sizeof(next));}
};
Trie *root = NULL;
void Trie_insert(char *str,char *tmp)
{
Trie *p = root;
for(int i=0;i<strlen(str);i++)
{
if(p -> next[str[i] - 'a' ]== NULL)
{
p -> next[str[i] - 'a'] = new Trie;
}
p = p ->next[str[i] - 'a'];
}
p->cnt++;//走到字符串的最后一个字母的节点
strcpy(p->s,tmp);
p->flag=1;
}
void Trie_search(char *str)
{
Trie *p = root;
for(int i=0;i<strlen(str);i++)
{
if(p -> next[str[i] - 'a'] == NULL)
{
cout<<"eh"<<endl;
return;
}
else p = p -> next[str[i] - 'a'];
}
cout<<p ->s<<endl;
}
int main()
{
char str[30],s1[15],s2[15];
root = new Trie;
while(gets(str))
{
if(str[0] == '\0')
break;
sscanf(str,"%s %s",s1,s2);
Trie_insert(s2,s1);
}
while(scanf("%s",str)!=EOF)
Trie_search(str);
return 0;
}
//map:
#include <map>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
char str[30],s1[15],s2[15];
root = new Trie;
while(gets(str))
{
if(str[0] == '\0')
break;
sscanf(str,"%s %s",s1,s2);
Trie_insert(s2,s1);
}
while(scanf("%s",str)!=EOF)
Trie_search(str);
return 0;
}
int main()
{
map<string,string> m;
string s1,s2,s;
while(getline(cin,s))
{
if(s == "")
break;
s1 = "";
s2 = "";
int sum = 0;
for(int i=0;i<s.size();i++)
{
if(s[i] == ' ')
break;
s1 += s[i];
++sum;
}
sum +=1;
for(;sum<s.size();sum++)
{
s2 += s[sum];
}
m[s2] = s1;
}
while(cin>>s)
{
if(m[s] != "")
cout<<m[s]<<endl;
else cout<<"eh"<<endl;
}
m.clear();
return 0;
}
还没有评论,来说两句吧...