您的位置:首页 > 其它

HDU 1075 What Are You Talking About(字典树)

2017-08-07 18:46 411 查看


What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)

Total Submission(s): 24125    Accepted Submission(s): 8075


Problem Description

Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help
him?

 

Input

The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains
two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single
line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new
word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single
string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.

 

Output

In this problem, you have to output the translation of the history book.

 

Sample Input

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END

 

Sample Output

hello, i'm from mars.
i like earth!

Hint
Huge input, scanf is recommended.

 

Author

Ignatius.L

 

在每一个字符串的最后记录下对应的标号。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
#define  LL long long
const int N = 1000000;
int tree
[27];
int aim
;
int sz=1;
char indx
[11];
char s[20];
void init()
{
memset(tree,0,sizeof tree);
memset(aim,0,sizeof aim);
sz=1;
}
void build(int x,char ss[])
{
int u=0;
int l=strlen(ss);
for(int i=0;i<l;i++)
{
int j=ss[i]-'a';
if(tree[u][j]==0)
{
tree[u][j]=sz++;
}
u=tree[u][j];
}
aim[u]=x;

}
int query(char ss[])
{
int u=0;
int l=strlen(ss);
for(int i=0;i<l;i++)
{
int j=ss[i]-'a';
if(tree[u][j]==0)
return 0;
u=tree[u][j];
}
return aim[u];
}
int main()
{
scanf("%s",s);
int cnt=0;
while(scanf("%s",indx[++cnt]))
{
if(strcmp(indx[cnt],"END")==0)
{
break;
}
scanf("%s",s);
build(cnt,s);
}
scanf("%s\n",s);
char c;
cnt=0;
while(c=getchar())
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
{
s[cnt++]=c;
}
else
{
s[cnt]='\0';
cnt=0;
if(strcmp(s,"END")==0&&c=='\n') break;
int k=query(s);
if(k) printf("%s%c",indx[k],c);
else printf("%s%c",s,c);
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: