标签:
Description
Input
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!
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 typedef struct Trie 5 { 6 int num; 7 struct Trie *next[26]; 8 bool exist; 9 char ss[11]; 10 }Node,*trie; 11 Node *create() 12 { 13 Node* node=(Node *)malloc(sizeof(Node)); 14 node->num=0; 15 node->exist=false; 16 memset(node->next,0,sizeof(node->next)); 17 return node; 18 } 19 void insert_(trie root,char *mars,char *en) 20 { 21 trie node=root; 22 char *p=mars; 23 int id; 24 while(*p) 25 { 26 id=*p-‘a‘; 27 if(node->next[id]==NULL) 28 { 29 node->next[id]=create(); 30 } 31 node=node->next[id]; 32 p++; 33 node->num+=1; 34 } 35 node->exist=true; 36 strcpy(node->ss,en); 37 } 38 char* finds(trie root,char *str) 39 { 40 trie node=root; 41 char *p=str; 42 int id; 43 while(*p) 44 { 45 id=*p-‘a‘; 46 node=node->next[id]; 47 ++p; 48 if(node==NULL) 49 return NULL; 50 } 51 if(node->exist){ 52 //printf("%p\n",node->ss); 53 return node->ss; 54 } 55 else 56 return NULL; 57 } 58 int main() 59 { 60 trie root=create(); 61 char str1[3001],str2[11],str[3001],*p; 62 while(scanf("%s",str1)) 63 { 64 if(strcmp(str1,"START")==0) 65 continue; 66 else if(strcmp(str1,"END")!=0){ 67 scanf("%s",str2); 68 insert_(root,str2,str1); 69 } 70 else break; 71 } 72 getchar(); 73 while(gets(str1)) 74 { 75 int j=0; 76 if(strcmp(str1,"START")==0) 77 continue; 78 if(strcmp(str1,"END")==0) 79 break; 80 for(int i=0;str1[i]!=‘\0‘;i++) 81 { 82 if(str1[i]>=‘a‘&&str1[i]<=‘z‘) 83 str[j++]=str1[i]; 84 else 85 { 86 str[j]=‘\0‘; 87 p=finds(root,str); 88 if(p) 89 { 90 printf("%s",p); 91 } 92 else 93 printf("%s",str); 94 j=0; 95 printf("%c",str1[i]); 96 } 97 } 98 printf("\n"); 99 } 100 return 0; 101 }
HDU 1075 What Are You Talking About
标签:
原文地址:http://www.cnblogs.com/PrayG/p/5899631.html