字符串哈希水题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
#define debug cout<<"***"<<endl;
map <int,int> m1,m2;
const int maxn=105000;
char spell[maxn][100];
char magic[maxn][100];
int f1[maxn];
int f2[maxn];
int pos1=1,pos2=1;
unsigned int BKDRHash(char *str)
{
unsigned int seed = 131;
unsigned int hash = 0;
while (*str){
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
void init(){
for(int i=1;i<pos1;i++){
m1[BKDRHash(magic[i])]=i;
m2[BKDRHash(spell[i])]=i;
//f1[BKDRHash(magic[i])]=i;debug;
//f2[BKDRHash(spell[i])]=i;
}
}
int main(){
memset(f1,-1,sizeof(f1));
memset(f2,-1,sizeof(f2));
while(scanf("%s",magic[pos1])){
if(!strcmp(magic[pos1],"@END@"))
break;
getchar();
gets(spell[pos2]);
pos1++;
pos2++;
}
init();
int t;
scanf("%d",&t);
getchar();
while(t--){
char str[100];
gets(str);
if(str[0]=='['){
int tmp=m1[BKDRHash(str)];
if(tmp==0)
printf("what?\n");
else
printf("%s\n",spell[tmp]);
}
else{
int tmp=m2[BKDRHash(str)];
if(tmp==0)
printf("what?\n");
else
for(int i=1;i<strlen(magic[tmp])-1;++i)
printf("%c",magic[tmp][i]);
printf("\n");
}
}
return 0;
}