HDU 1075 字典树
2013-08-06 01:20
218 查看
What Are You Talking About
[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!
建树,查找,只要主函数处理得好就很水了..
#include <iostream> using namespace std; struct node { node *next[26]; char *str; node()//初始化 { memset(next,0,sizeof(next)); str=NULL; } }; node *root=NULL; void build(char *s1,char *s2)//建树 { node *p=root; int i,len=strlen(s2); for(i=0;i<len;i++) { if(p->next[s2[i]-'a']==NULL) p->next[s2[i]-'a']=new node; p=p->next[s2[i]-'a']; } p->str=new char[22]; strcpy(p->str,s1); } void findstr(char *s)//查找 { node *p=root; int i,len=strlen(s); if(len==0)return; for(i=0;i<len;i++) { if(s[i]>'z'||s[i]<'a'||p->next[s[i]-'a']==NULL) { printf("%s",s); return; } else p=p->next[s[i]-'a']; } if(p->str!=NULL) printf("%s",p->str); else printf("%s",s); } int main() { root=new node; char s[11],s1[111],s2[111],str[1111],ss[111]; scanf("%s",s); while(~scanf("%s",s1)) { if(strcmp(s1,"END")==0)break; scanf("%s",s2); build(s1,s2); } scanf("%s%*c",s); while(gets(str)) { if(strcmp(str,"END")==0)break; int i,len=strlen(str); int k=0; bool flag=false; for(i=0;i<len;i++)//拆分成多个单词逐一查找 { if(str[i]>='a'&&str[i]<='z') { if(flag==false) flag=true; ss[k++]=str[i]; } else { if(flag==true) { flag=false; ss[k]='\0'; findstr(ss); k=0; } printf("%c",str[i]); } } if(flag==true) { ss[k]='\0'; findstr(ss); } printf("\n"); } return 0; }
相关文章推荐
- HDU 1075 What Are You Talking About (map 或者 字典树)
- HDU 1075字典树(水题)
- hdu 1075 字典树
- hdu 1075 字典树
- hdu 1075 字典树
- HDU 1075 What Are You Talking About (STL, 字典树)
- HDU 1075 字典树
- HDU - 1075 - What Are You Talking About(字典树)
- Hdu 1075 - What Are You Talking About(字典树)
- hdu 1075 字典树
- HDU 1075 What Are You Talking About (字典树)
- HDU 1075 字典树 树搜索 逆路径输出单词
- hdu 1075 What Are You Talking About(HashMap水过,可用字典树)
- HDU 1075 翻译 (字典树||map)
- 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【字典树】or【字符串map】
- hdu 1075 字典树
- HDU 1075(简单字典树)