标签:des style blog java color strong
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 12773 Accepted Submission(s): 4069
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <map> 5 #include <algorithm> 6 using namespace std; 7 8 main() 9 { 10 map<string,string>ma; 11 char a[15], b[15], s[3005]; 12 int n, i, j, k; 13 scanf("START"); 14 while(scanf("%s",a)&&strcmp(a,"END")!=0) 15 { 16 scanf("%s",b); 17 ma[b]=a; 18 } 19 scanf("%*s"); 20 getchar(); 21 while(gets(s)&&strcmp(s,"END")!=0) 22 { 23 k=0;n=strlen(s); 24 for(i=0;i<n;i++) 25 { 26 if(s[i]>=‘a‘&&s[i]<=‘z‘) 27 b[k++]=s[i]; 28 else{ 29 b[k]=‘\0‘; 30 k=0; 31 if(ma.find(b)!=ma.end()) 32 cout<<ma[b]; 33 else cout<<b; 34 printf("%c",s[i]); 35 } 36 } 37 if(s[i-1]>=‘a‘&&s[i-1]<=‘z‘) 38 { 39 b[k]=‘\0‘; 40 cout<<ma[b]; 41 } 42 printf("\n"); 43 } 44 45 }
字典树代码:
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <iostream> 5 using namespace std; 6 7 char a[1000005][15]; 8 9 struct node{ 10 int id; 11 struct node *next[26]; 12 node(){ 13 id=-1; 14 memset(next,0,sizeof(next)); 15 } 16 }root; 17 18 19 void insert(char *s,int id) 20 { 21 struct node *p; 22 p=&root; 23 int i=0; 24 while(s[i]) 25 { 26 if(p->next[s[i]-‘a‘]==0) { 27 p->next[s[i]-‘a‘]=new node; 28 p=p->next[s[i]-‘a‘]; 29 } 30 else p=p->next[s[i]-‘a‘]; 31 i++; 32 } 33 p->id=id; 34 } 35 36 37 char *find(char *s) 38 { 39 struct node *p; 40 p=&root; 41 int i=0; 42 while(s[i]&&p->next[s[i]-‘a‘]) 43 { 44 p=p->next[s[i]-‘a‘]; 45 i++; 46 } 47 if(!s[i]&&p->id!=-1) 48 return a[p->id]; 49 else return s; 50 } 51 52 main() 53 { 54 char b[3005], c[3005]; 55 int i, j, k=0; 56 scanf("START"); 57 while(scanf("%s",a[k])&&strcmp(a[k],"END")!=0) 58 { 59 scanf("%s",b); 60 insert(b,k); 61 k++; 62 } 63 scanf("%s%*c",c); 64 while(gets(b)) 65 { 66 if(strcmp(b,"END")==0) break; 67 i=0; k=0; 68 for(i=0;b[i];i++) 69 { 70 if(b[i]>=‘a‘&&b[i]<=‘z‘) 71 c[k++]=b[i]; 72 else{ 73 c[k]=‘\0‘; 74 k=0; 75 printf("%s",find(c)); 76 printf("%c",b[i]); 77 } 78 } 79 if(b[i-1]>=‘a‘&&b[i-1]<=‘z‘) 80 { 81 c[k]=‘\0‘; 82 printf("%s",find(c)); 83 } 84 85 printf("\n"); 86 } 87 }
对于本题,字典树效率比map高出很多。
HDU 1075 map or 字典树,布布扣,bubuko.com
标签:des style blog java color strong
原文地址:http://www.cnblogs.com/qq1012662902/p/3831625.html