您的位置:首页 > 其它

hdu----(1075)What Are You Talking About(trie之查找)

2014-09-17 20:32 411 查看

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 14107 Accepted Submission(s): 4546


[align=left]Problem Description[/align]
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?

[align=left]Input[/align]
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.

[align=left]Output[/align]
In this problem, you have to output the translation of the history book.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

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

Hint

Huge input, scanf is recommended.

[align=left]Author[/align]
Ignatius.L

[align=left]Recommend[/align]
[align=left] [/align]
[align=left] [/align]
[align=left]就是给定一个火星的文字,然后对应着有地球的翻译,然后给定一段包含有火星文和地球文的文字,要你翻译成为地球文(如果是地球文,这直接输出,反之则翻译成为地球文。)[/align]
[align=left]思路: 其实只需要将火星文做成一颗字典树(而且在每一个单词的末尾,填上一个tail,使其能够进行对应的查找。),然后对于每一个单词进行一次查找,如果有,则直接翻译,如果没有则直接输出。[/align]
[align=left] [/align]
[align=left]代码:[/align]

/*hdu 1075 字典树写法*/
//#define LOCAL
#include<cstdio>
#include<cstring>
#include<cstdlib>
typedef struct node
{
int id;
struct node *child[26];
}Trie;
void Insert(char *s,Trie *root,int v)
{
Trie *cur=root,*curnew;
int i,pos;
for(i=0;s[i]!='\0';i++){
pos=s[i]-'a';
if(cur->child[pos]==NULL)
{
curnew= new Trie;
curnew->id=0;
for(int j=0;j<26;j++)
curnew->child[j]=NULL;
cur->child[pos]=curnew;
}
cur=cur->child[pos];
}
cur->id=v;
}
int query(char *s,Trie *root)
{
Trie *cur=root;
int i,pos;
for(i=0;s[i]!='\0';i++){
pos=s[i]-'a';
if(cur->child[pos]!=NULL)
cur=cur->child[pos];
else return 0; //输出原字符串
}
return cur->id;
}
char aa[700000][11],bb[11];
char str[3010];
int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
Trie *root= new Trie ;
root->id=0;
int st;
for(int i=0;i<26;i++)
root->child[i]=NULL;
scanf("%s",aa[0]);
int i=1;
while(scanf("%s",aa[i]),strcmp(aa[i],"END"))
{
scanf("%s",bb);
Insert(bb,root,i);  //对应标号
i++;
}
scanf("%s",aa[0]);
getchar();
while(gets(str),strcmp(str,"END"))
{
for(st=i=0;str[i]!='\0';i++)
{
if(str[i]<'a'||str[i]>'z'){
if(i>st){
strncpy(aa[0],str+st,i-st);
aa[0][i-st]='\0';
printf("%s",aa[query(aa[0],root)]);
}
st=i+1;
printf("%c",str[i]);
}
}
puts("");
}
return 0;
}


当然可以用map,在这里就不在补充啦!喵喵!O(∩_∩)O哈哈~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: