1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<cmath> #include<map> using namespace std;
typedef long long ll;
struct Trie{ Trie *next[26]; int val; Trie(){ val = -1; for (int i = 0 ; i < 26 ; i ++) next[i] = NULL; } };
void addword(char *str,Trie* node,int i) { if (node->next[str[0]-'a']==NULL) { node->next[str[0]-'a'] = new Trie; node = node->next[str[0]-'a']; } else { node = node->next[str[0]-'a']; } str++; if (*str) addword(str,node,i); else { node->val = i; return; } }
int query(char *str ,Trie *node) { if (node->next[*str-'a']==NULL) return -1; else node= node->next[*str-'a']; ++str; if (*str) return query(str,node); else return node->val; }
char str[4000]; char temp[4000],tw[4000]; char word[1200000][20];
int main() { int T,n,i; int cnt = 0; Trie* head = new Trie; scanf("%s",str); getchar(); gets(str); while (strcmp(str,"END")!=0) { for (i = 0 ; ; i ++) { if (str[i]==' ') { word[cnt][i++] = 0; break; } else word[cnt][i] = str[i]; } cnt++; addword(str+i,head,cnt-1); gets(str); } scanf("%s",str); getchar(); gets(str); int lw; while (strcmp(str,"END")!=0) { cnt =0; int tp = 0; memset(temp,0,sizeof(temp)); for (i = 0; str[i]; i ++) { if (str[i]>='a'&&str[i]<='z') { tw[cnt++] = str[i]; } else { if (cnt!=0) { tw[cnt] = 0; int pos = query(tw,head); if (pos!=-1) { strcat(temp,word[pos]); tp += strlen(word[pos]); } else { strcat(temp,tw); tp += strlen(tw); } cnt = 0; } temp[tp++] = str[i]; } } if (cnt!=0) { tw[cnt] = 0; int pos = query(tw,head); if (pos!=-1) { strcat(temp,word[pos]); tp += strlen(word[pos]); } else { strcat(temp,tw); tp += strlen(tw); } cnt = 0; } temp[tp] = 0; printf("%s\n",temp); gets(str); }
return 0; }
|