【HDOJ】1075 What Are You Talking About
map,STL搞定。
1 #include <iostream> 2 #include <string> 3 #include <cstdio> 4 #include <cstring> 5 #include <map> 6 using namespace std; 7 8 #define MAXN 3005 9 10 char buf[MAXN], word[15]; 11 12 int main() { 13 map<string, string> dict; 14 map<string, string>::iterator it; 15 int i, j; 16 17 scanf("%*s"); // START 18 while (scanf("%s", buf)!=EOF && strcmp(buf, "END")) { 19 scanf("%s", word); 20 dict[word] = buf; 21 } 22 23 scanf("%*s%*c"); // START 24 while (gets(buf)!=NULL && strcmp(buf, "END")) { 25 for (i=0, j=0; i<strlen(buf); ++i) { 26 if (buf[i]>='a' && buf[i]<='z') { 27 word[j++] = buf[i]; 28 } else { 29 // If there is a valid word, find in dict. 30 if (j) { 31 word[j++] = '\0'; // add '\0' 32 it = dict.find(word); 33 if (it != dict.end()) 34 printf("%s", (*it).second.data()); 35 else 36 printf("%s", word); 37 j = 0; 38 } 39 printf("%c", buf[i]); 40 } 41 } 42 printf("\n"); 43 } 44 45 return 0; 46 }