What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 18931 Accepted Submission(s): 6199
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
hello, i'm from mars.i like earth!
题意:START开始END结束,先给你一个字典。前面的字符串和后面的字符串互相对应。
然后还是START开始END结束给你一句话,将里面在字典里出现过的单词换成英文单词。
字典树模板,水题。
代码:#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef struct Tree { struct Tree *next[26]; int v; char op[11]; }; Tree *root; void update(char *a,char *b) { Tree *p=root,*q; int len=strlen(b); for(int i=0; i<len; i++) { int k=b[i]-'a'; if(p->next[k]==NULL) { q=new Tree; for(int j=0; j<26; j++) q->next[j]=NULL; q->v=0; p->next[k]=q; } p=p->next[k]; } p->v=-1; int len1=strlen(a); for(int i=0; i<len1; i++) p->op[i]=a[i]; p->op[len1]='\0'; } int finds(char *a) { Tree *p=root; for(int i=0; i<strlen(a); i++) { int j=a[i]-'a'; if(p->next[j]==NULL) return 1; p=p->next[j]; } if(p->v!=-1) return 1; printf("%s",p->op); return 0; } int main() { root=new Tree; for(int i=0; i<26; i++) root->next[i]=NULL; root->v=0; char a[15],b[15],c[1000];//这里题目没说清楚后面给的句子长度,一开始写成c[100]WA了。 scanf("%s",a); while(scanf("%s",a)) { if(strcmp(a,"END")==0) break; scanf("%s",b); update(a,b); } scanf("%s",a); getchar(); while(gets(c)) { if(strcmp(c,"END")==0) break; int k=0; for(int i=0; i<strlen(c); i++) { if(c[i]>='a'&&c[i]<='z') { a[k++]=c[i]; if(i==strlen(c)-1) { a[k]='\0'; if(finds(a)) printf("%s",a); } } else { a[k]='\0'; if(finds(a)) printf("%s",a); printf("%c",c[i]); k=0; } } printf("\n"); } return 0; }