POJ 2503 Babelfish

来源:互联网 发布:淘宝上网络解锁可靠吗 编辑:程序博客网 时间:2024/06/11 14:31
Babelfish
Time Limit: 3000MS Memory Limit: 65536KTotal 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 ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops

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;}


0 0
原创粉丝点击