解题思路:字典树
#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 ;
}