字符串哈希

给定N个字符串(第i个字符串长度为Mi,字符串内包含数字、大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串。

先讲超短非hash写法,容易tle

string s[N];
sort(s+1,s+1+n);
cout<<unique(s+1,s+1+n)-s-1;

单hash(没有mod,自然溢出)

#define ull unsigned long long
const int o=1524,oo=10024;
int num[o],link[oo];
char s[o][oo];
ull base=131,hash[oo];

For(i,1,n){
scanf("%s",s[i]+1);
int len=strlen(s[i]+1);
link[len][++num[len]]=i;
ull res=0;
For(j,1,ln)
res=res*base+(ull)(s[i][j]-'0');
hash[i]=res;//记录
For(j,1,num[len]-1){
if(hash[link[i][j]]==res){
ans++;
break;
}}
cout<<n-ans;

 
原文地址:https://www.cnblogs.com/planche/p/9410686.html