1 #include<cstdio> 2 const int maxn = 100; 3 char S[maxn][5], temp[5]; 4 int hashTable[52 * 52 * 52 + 10] = {0}; 5 //将52个字母对应到52进制中去。 6 //将A--Z对应到0--25,将a---z可作为26--52. 7 int hashFunc(char s[], int len){ 8 int id = 0; 9 for (int i = 0; i <len; i++) 10 { 11 if(s[i]>='A'&&s[i]<='Z') 12 { 13 id = id * 52 + (s[i] - 'A'); 14 } 15 else if(s[i]>='a'&&s[i]<='z') 16 { 17 id = id * 52 + (s[i] - 'a'); 18 } 19 } 20 return id;//由此返回的十进制的数便与每个字符串一一对应了。 21 } 22 int main() 23 { 24 int n, m; 25 printf("请分别输入两字符串组N与M所含的个数"); 26 scanf("%d%d", &n, &m); 27 for (int i = 0; i < n; i++) 28 { 29 scanf("%s", &S[i]); 30 int id = hashFunc(S[i], 3); 31 hashTable[id]++; 32 } 33 for (int i = 0; i < m; i++)//对于每个M组中的字符串 34 { 35 scanf("%s", temp); 36 int id = hashFunc(temp, 3); 37 printf("%d\n", hashTable[id]); 38 } 39 return 0; 40 }