您的位置:首页 > 其它

POJ-2503-Babelfish

2014-08-04 16:01 357 查看
Babelfish

Time Limit: 3000MS  Memory Limit: 65536K

Total Submissions: 32156  Accepted: 13820

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

Waterloo local 2001.09.22

 

 

 

题意:

给你翻译和原词,让你输入原词能输出相应的翻译,如果没有对应的翻译,输出“eh”

思路:map会超时,Hash还没有深入研究,用字典(trie)树做过了

AC代码:

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define INF 0x11111110
using namespace std;
struct node
{
node *next[26];//node类的26个子类
char str[15];
node(){//构造函数,用来初始化数据
memset(next,0,sizeof(next));
memset(str,0,sizeof(str));
}
};
node *root=new node();//声明一个初始node类 (这个类决定了以下所有子类,是字典树的根)
void insert(char *s,char *t)//插入新的字符串
{
node *p=root;
int i,k;
for(i=0;s[i];i++){
k=s[i]-'a';
if(p->next[k]==NULL) p->next[k]=new node();//不存在此节点则创建
p=p->next[k];//移向下一个节点
}
strcpy(p->str,t);
}
char *Find(char *s)
{
node *p=root;
int i,k,n;
n=strlen(s);
for(i=0;i<n;i++){
k=s[i]-'a';
if(p->next[k]!=NULL)
p=p->next[k];//移向下一个节点
else
return "eh";
}
return p->str;
}
char str[40],str1[20],str2[20];
int main()
{
int i,j,n,m,x;
while(gets(str))//注意输入,比较坑
{

if(strcmp(str,"")==0)
{
memset(str,0,sizeof(str));
break;
}
n=strlen(str);
for(i=0;i<n&&str[i]!=' ';i++)
{
str1[i]=str[i];
}
i++;x=0;
for(;i<n;i++)
{
str2[x++]=str[i];
}
insert(str2,str1);
memset(str1,0,sizeof(str1));
memset(str2,0,sizeof(str2));
memset(str,0,sizeof(str));
}
while(scanf("%s",str)!=EOF)
{
printf("%s\n",Find(str));
memset(str,0,sizeof(str));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: