HDU 1075——What Are You Talking About(Trie树)
2014-10-09 01:09
465 查看
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 14180 Accepted Submission(s): 4567
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
—————————————————————分割线————————————————
题目大意:
给一个字典和要翻译的句子,对句子进行翻译
思路:
对火星文构建Trie树,并保存对应的译文,然后对要翻译的句子进行操作
原来用string RE了,后来改为char
#include<iostream> #include<cstring> #include<cstdio> #include<cstdlib> #include<string> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=3001; const int sigma_size=26; using namespace std; char line[30],s1[15],s2[15],str[maxn]; char dic[1000000][12]; int cnt; struct Trie { int val; Trie *next[sigma_size]; Trie():val(0){memset(next,0,sizeof(next));} }; Trie *root; void insert(char *s) { Trie *p=root,*q; int l=strlen(s); for(int i=0;i<l;++i){ int c=s[i]-'a'; if(p->next[c]==NULL){ p->next[c]=new Trie; } p=p->next[c]; } p->val=cnt; } int query(char *s) { Trie *p=root; int l=strlen(s); for(int i=0;i<l;++i){ int c=s[i]-'a'; if(p->next[c]==NULL) return -1; p=p->next[c]; } if(p->val) return p->val; return -1; } int main() { cnt=1; root=new Trie; while(gets(line)){ if(strcmp(line,"START")==0) continue; if(strcmp(line,"END")==0) break; sscanf(line,"%s%s",s1,s2); insert(s2); strcpy(dic[cnt++],s1); } while(gets(str)){ if(strcmp(str,"START")==0) continue; if(strcmp(str,"END")==0) break; int l=strlen(str); int st=0; for(int i=0;i<l;++i){ if(isspace(str[i])||ispunct(str[i])){ s1[st]='\0'; int index=query(s1); if(index!=-1) printf("%s",dic[index]); else printf("%s",s1); printf("%c",str[i]); st=0; }else{ s1[st++]=str[i]; } } printf("\n"); } return 0; }
#include <cstdio> #include <string.h> #include <cstdlib> using namespace std; char line[30],s1[12],s2[12],tline[3010]; struct trie { trie *next[26]; char c[12]; int has; trie():has(0){memset(next,0,sizeof(next));} }*root; void instrie(char s11[12],char s12[12]) { int len=strlen(s12); trie *p=root; for(int i=0; i<len; i++) { int t=s12[i]-'a'; if(p->next[t]==NULL)p->next[t]=new trie; p=p->next[t]; } p->has=1; strcpy(p->c,s11); } bool search(char s11[12]) { int len=strlen(s11); trie *p=root; for(int i=0; i<len; i++) { int t=s11[i]-'a'; if(p->next[t]==NULL)return false; p=p->next[t]; } if(p->has==0)return false; strcpy(s2,p->c); return true; } int main() { root=new trie; while(gets(line)) { if(strcmp(line,"START")==0)continue; if(strcmp(line,"END")==0)break; sscanf(line,"%s%s",s1,s2); instrie(s1,s2); } while(gets(tline)) { if(strcmp(tline,"START")==0)continue; if(strcmp(tline,"END")==0)break; int i=0,j=0,len=strlen(tline); while(i<len) { if(tline[i]<'a'||tline[i]>'z') { s1[j]='\0'; if(search(s1))printf("%s",s2); else printf("%s",s1); printf("%c",tline[i]); j=0,i++; } else { s1[j]=tline[i]; i++,j++; } } printf("\n"); } }
相关文章推荐
- hdu 1075 What Are You Talking About(Trie树)
- HDU 1075 What are you talking about(trie树)
- hdu 1075 What Are You Talking About(Trie树)
- hdu 1075 What Are You Talking About(Trie树)
- hdu 1075 What Are You Talking About (Trie树,水题)
- hdu 1075 What Are You Talking About(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(字典树)
- HDU-1075 What Are You Talking About(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【字典树 & STL】
- [字典树] 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 字典树