传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2946
hash+二分大水题不说了
其实主要是用来测试一下我的新笔记本,真是爽!!!
还是不贴渣配置了吧- -|
Code:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long UL;
int n,mn=2001,len[6];
UL base=233;
UL hash[6][2010],hash_l[2010];
char s[2010];
UL hshs(int i,int l,int r){
// cerr<<hash[i][r]-hash[i][l-1]*hash_l[r-l+1]<<endl;
return hash[i][r]-hash[i][l-1]*hash_l[r-l+1];
}
set<UL>S1,S2;
bool ok(int l){
S1.clear();S2.clear();
for(int i=1;i+l-1<=len[1];i++)S1.insert(hshs(1,i,i+l-1));
for(int i=2;i<=n;i++){
for(int j=1;j+l-1<=len[i];j++){
UL h=hshs(i,j,j+l-1);
if(S1.count(h))S2.insert(h);
}S1=S2;S2.clear();
}return S1.size();
}
int main(){
scanf("%d",&n);
hash_l[0]=1;
for(int i=1;i<=2000;i++)hash_l[i]=hash_l[i-1]*base;
for(int i=1;i<=n;i++){
scanf("%s",s+1);
UL val=0;len[i]=strlen(s+1);mn=min(mn,len[i]);
for(int j=1;j<=len[i];j++)
hash[i][j]=hash[i][j-1]*base+s[j]-'a';
}
int l=0,r=mn+1;
while(l<r){
int mid=(l+r)>>1;
if(ok(mid))
l=mid+1;
else r=mid;
}cout<<l-1<<endl;
return 0;
}