//字符串hash练手题,采用拉链式解决冲突 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int M = 43853;//hash表大小 struct node {//节点 char str1[11]; char str2[11]; node *next; }; struct hashTable {//hash表 node *link; }hash[M]; char text[3005]; char str1[11], str2[11], ch[11], ans[11]; void init() {//初始化 for (int i=0; i<M; ++i) hash[i].link = NULL; return ; } unsigned int BKDRHash(char * str) {//hash unsigned int seed = 131; unsigned int hash = 0; while (*str) hash = hash * seed + (*str++); return hash & 0x7FFFFFFF; } void insert(char *str1, char *str2) {//插入 int k = BKDRHash(str1) % M; node *p = new node(); strcpy(p->str1, str1); strcpy(p->str2, str2); p->next = hash[k].link; hash[k].link = p; return ; } int find(char *str) { //查找 int k = BKDRHash(str) % M; node *p = hash[k].link; while (p) { if (!strcmp(p->str1, str)) { strcpy(ans, p->str2); return 1; //找到 } p = p->next; } return 0; // 没找到 } int main() { init(); scanf ("%s", str1); while (scanf("%s", str1), strcmp(str1, "END")!=0) { scanf ("%s", str2); insert(str2, str1); } scanf ("%s", str1); getchar(); while (gets(text), strcmp(text, "END")!=0) { int l = strlen(text); for (int i=0; i<l;) { int k = 0; if (text[i]>='a' && text[i]<='z') { int j = i; while (text[j]>='a' && text[j]<='z' && j<l) ch[k++] = text[j++]; i = j; ch[k] = '\0'; if (find(ch)) printf ("%s", ans); else printf ("%s", ch); } else printf ("%c", text[i++]); } puts(""); } return 0; }