您的位置:首页 > 其它

POJ 2503 Babelfish

2010-12-13 12:46 417 查看
解题思路:字典树

NULL

#include <iostream>
using namespace std;
#define MAXN 1000001
char dic[MAXN],word[MAXN][11],ch[11];
int size,first[MAXN],next[MAXN],place[MAXN];

void Build(int n)
{
int i,t,p=0,l=strlen(ch);
for(i = 0; i<l;i++)
{
t=first[p];
while (t&&dic[t]!=ch[i])t=next[t];
if(!t)
{
dic[++size]=ch[i],next[size]=first[p];
first[p]=size,first[size]=0,p=size;
}
else
p=t;
}
place[p]=n;
}
int main()
{
int i,t,p,l,n,size=n=0;
char str[30];
while (gets(str)&&str[0]!='\0')
sscanf(str, "%s %s",word[++n], ch),Build(n);
while (gets(ch)&&ch[0]!='\0')
{
p=0,l=strlen(ch);
for(i=0;i<l;i++){t=first[p];while (t&&dic[t]!=ch[i])t=next[t];p=t;}
(p==0)?printf("eh\n"):printf("%s\n",word[place[p]]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: