外地人

来源:互联网 发布:amd多核cpu优化补丁 编辑:程序博客网 时间:2024/06/08 05:38

【Trie】外地人

Time Limit:30000MS  Memory Limit:165536K
Case Time Limit:3000MS

Description

你从融侨山区考入大城市沙坪坝的学校,但是沙坪坝的当地人说着一种很难懂的方言,你完全听不懂。幸好你手中有本字典可以帮你。现在你有若干个听不懂的方言需要查询字典。 

Input

第一行,两个整数n和m。 
接下来有n行表示字典的内容,每行表示一条字典的记录。每条记录包含两个空格间隔的单词,第一个单词为英文单词,第二个单词为对应的沙坪坝方言。 
接下来有m行,每行一个单词,表示你要查询的沙坪坝方言。 

Output

输出m行,每行一个英文单词,表示翻译后的结果。 
如果某个单词字典查不到,输出"eh" 

Sample Input

5 3dog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslay

Sample Output

catehloops

Hint

Huge input and output,scanf and printf are recommended. 

n,m<=100000 

单词的长度不超过10

Source

Waterloo local 2001.09.22 poj2503


#include <cstdio>#include <cstring>struct node{int num, next[26];}trie[1000001];char en[100001][12], sh[12];int tot=1;void insert(int point){int len=strlen(sh), p=1;for(int i=0; i<len; i++){int t=sh[i]-'a';if(trie[p].next[t]==0){tot++;trie[p].next[t]=tot;p=trie[p].next[t];trie[p].num=0;}else{p=trie[p].next[t];}}trie[p].num=point;}int find(){int p=1, len=strlen(sh);for(int i=0; i<len; i++){int t=sh[i]-'a';if(trie[p].next[t]==0)return 0;p=trie[p].next[t];}return trie[p].num;}int main(){int n, m, ans;en[0][0]='e';en[0][1]='h';en[0][2]='\0';scanf("%d%d", &n, &m);for(int i=1; i<=n; i++){scanf("%s%s", en[i], sh);insert(i);}while(m--){scanf("%s", sh);printf("%s\n", en[find()]);}}


0 0
原创粉丝点击