题目描述
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 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 is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
样例输入
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
样例输出
cat
eh
loops
提示
Huge input and output,scanf and printf are recommended.
这道题,可以用map做,map里的查询可以调用自带函数,也可以自己写结构体,用二分实现map;然而,我写了几次,一直存在bug,没办法,套用字典树模板ac过的。字典树,时间上应该更优。
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mpxx=100050;
char s[mpxx][20],c[20];
struct node
{
int flag;
node *next[26];
}*head;
int top;
node *creat()//初始化字典树
{
node *p;
p=new node;
p->flag=0;
for(int i=0;i<26;i++)
{
p->next[i]=NULL;
}
return p;
}
void build()//字典树的构建
{
node *p=head;//p是不断更新的节点
int a,len=strlen(c);
for(int i=0;i<len;i++)
{
a=c[i]-'a';
if(!p->next[a])
{
p->next[a]=creat();
}
p=p->next[a];
}
p->flag=top;
}
int query()//查询操作
{
node *p=head;
int a,len=strlen(c);
for(int i=0;i<len;i++)
{
a=c[i]-'a';
if(!p->next[a])
{
return 0;
}
p=p->next[a];
}
if(!p->flag)
{
return 0;
}
else
{
return p->flag;
}
}
int main()
{
head=creat();
top=1;
int t;
char cc[40];
while(gets(cc)&&cc[0]!='\0')
{
sscanf(cc,"%s %s",s[top],c);
build();
top++;
}
while(scanf("%s",c)!=EOF)
{
t=query();
if(t)
{
printf("%s\n",s[t]);
}
else
{
printf("eh\n");
}
}
return 0;
}