字符串hash+bdkhash

code

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
BKDRHash算法了 种子可以选 31 131 1313 13131 131313

#define maxn 249997
using namespace std;
int hash[maxn],coun[maxn];
int ans;
unsigned int (char*str)
{
unsigned int seed=131 ;
unsigned int hash=0 ;
while(*str)
hash=hash*seed+(*str++);
return(hash % maxn);
}
void hashit(char *str)
{
int k,t;
while(*str=='0') str++;
k=BKDRHash(str);//返回一个整型的状态
t=k%maxn;
while(hash[t]!=-1&&hash[t]!=k) //重复处理
t=(t+10)%maxn;
if(hash[t]==-1) hash[t]=k,coun[t]=1;
else
{
coun[t]++;
if(coun[t]>ans) ans=coun[t];
}
}
int main()
{
int N;
char str[50];
while(scanf("%d",&N)!=EOF)
{
memset(hash,-1,sizeof(hash));
ans=1;
getchar();
while(N--)
{
gets(str);
hashit(str);
}
printf("%dn",ans);
}
}