Babelfish
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 29680 | Accepted: 12848 |
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 ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Sample Output
cat eh loops
Hint
Huge input and output,scanf and printf are recommended.
Source
题意:
给出一系列单词和译文,空行后开始询问译文,每个询问输出一个答案,如果译文能输出对应的单词,则输出这个单词,不能则输出 eh。
思路:
字典树。将对应单词输入到译文最后一个字母中,每次询问则查找字典树即可。
AC:
#include<cstdio>
#include<string.h>
using namespace std;
typedef struct no
{
struct no *next[30];
char sen[15];
}node;
node *creat_node()
{
node *p = new node;
for(int i = 0;i < 30;i++)
p -> next[i] = NULL;
memset(p -> sen,0,sizeof(p -> sen));
return p;
}
void insert_str(char *str,char *vab,node *head)
{
int len = strlen(str);
node *p = head;
for(int i = 0;i < len;i++)
{
int c = str[i] - 'a';
if(p -> next[c] == NULL)
p -> next[c] = creat_node();
p = p -> next[c];
}
strcpy(p -> sen,vab);
}
void search_str(char *str,node *head)
{
int len = strlen(str),i;
node *p = head;
for(i = 0;i < len;i++)
{
int c = str[i] - 'a';
if(p -> next[c] == NULL) break;
p = p -> next[c];
}
//除了判断i < len以外,还要判断长度,因为当这个译文为某字典译文的前缀时,最后一个字母没有存到单词信息
if(i < len || !strlen(p -> sen)) printf("eh\n");
else printf("%s\n",p -> sen);
}
int main()
{
char str[3005],vab[30],tra[30];
node *head = creat_node();
while(gets(str) && strlen(str))
{
int temp = 0,k = 0;
memset(vab,0,sizeof(vab));
memset(tra,0,sizeof(tra));
for(int i = 0;i < strlen(str);i++)
{
if(str[i] == ' ')
{
temp = 1;
vab[k] = '\0';
k = 0;
continue;
}
if(!temp) vab[k++] = str[i];
else tra[k++] = str[i];
}
tra[k] = '\0';
insert_str(tra,vab,head);
}
while(gets(str))
{
search_str(str,head);
}
return 0;
}