HDU 1075 What Are You Talking About
2014-02-10 02:03
525 查看
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 11638 Accepted Submission(s): 3722
[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]
[/align]
[align=left]
[/align]
[align=left]1,注意输出是每输入一行就要翻译一行,不是全部输入了在翻译。[/align]
[align=left]2,把火星文建成树,对应英文用数组存,num指向对应英文的序号。[/align]
[align=left]3,每个翻译并输出。[/align]
[align=left]
[/align]
#include <cstdio> #include <cstdlib> #include <cstring> char *zidian[1000000]; struct node { bool end; int num; node *x[26]; }; node *tree; char *ans[1010][3010]; int cal[1010][2]; char huoxing[3010]; int cnt1,cnt2; void create(node *&tree) { int i; tree=(node *)malloc(sizeof(node)); tree->end=false; //tree->num=-1;//注意-1,和-2的不同 tree->num=-2; for(i=0;i<26;++i) tree->x[i]=NULL; } void insertTree(int n) { node *f=tree; int i,j,len=strlen(huoxing); for(i=0;i<len;++i) { int p=huoxing[i]-'a'; if(f->x[p]==NULL) { f->x[p]=(node *)malloc(sizeof(node)); f=f->x[p]; f->end=false; //f->num=-1; f->num=-2; for(j=0;j<26;++j) f->x[j]=NULL; } else { f=f->x[p]; } } f->end=true; f->num=n; } char ch[20]; int searchTree() { node *f=tree; int i,len=strlen(ch); for(i=0;i<len;++i) { int p=ch[i]-'a'; if(f->x[p]==NULL) return -1; else f=f->x[p]; } if(f->end) return f->num; return -1; } int main() { int i,j; cnt1=0,cnt2=0; create(tree); while(scanf("%s",huoxing)&&strcmp(huoxing,"END")) { getchar(); if(strcmp(huoxing,"START")==0) continue; ++cnt1; zidian[cnt1]=(char *)malloc(sizeof(char)*10);//字典从1开始 strcpy(zidian[cnt1],huoxing); // printf("%s %d ",zidian[cnt1],cnt1); scanf("%s",huoxing); getchar(); // printf("%s\n",huoxing); insertTree(cnt1); } getchar(); while(gets(huoxing)&&strcmp(huoxing,"END"))//正文从1,1开始 { if(strcmp(huoxing,"START")==0) continue; int len=strlen(huoxing); i=0; int p=0; int flag; while(i!=len) { if(huoxing[i]>='a'&&huoxing[i]<='z') { p=-1; for(;i<len;++i) { if(huoxing[i]<'a'||huoxing[i]>'z') { break; } ++p; ch[p]=huoxing[i]; } ++p; ch[p]='\0'; flag=searchTree(); ++cnt2; if(flag==-1)//没有该火星文 { printf("%s",ch); } else { printf("%s",zidian[flag]); } } else { for(;i<len;++i) { if(huoxing[i]>='a'&&huoxing[i]<='z') { break; } printf("%c",huoxing[i]); } } } printf("\n"); } return 0; }
相关文章推荐
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About(字典树)
- HDU 1075 - What Are You Talking About
- HDU 1075 What Are You Talking About // 字符串映射,Trie
- hdu 1075 What Are You Talking About
- HDU 1075 What Are You Talking About
- (HDU)1075 - What Are You Talking About【字典树】or【字符串map】
- hdu 1075 What Are You Talking About(HashMap水过,可用字典树)
- hdu 1075 What Are You Talking About(map)
- HDU 1075-What Are You Talking About(Trie)
- HDU - 1075 - What Are You Talking About
- hdu 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About
- hdu 1075 What Are You Talking About(Trie树)
- HDU 1075 What Are You Talking About
- HDU 1075-What Are You Talking About(map)
- HDU 1075 What Are You Talking About
- HDU1075——What Are You Talking About
- hdu 1075 What Are You Talking About