| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
居然一次AC。。。。。#include<iostream>
using namespace std;
#define maxn 20000+200
#define prime 20011
int n,k,len,a[maxn],p[20],c[prime];
int f[maxn][16],Rand[prime],hash[prime];
int get_hash(int i,int l)
{
int j,h=0;
for (j=len; j>=0; j--) if (p[j]<=l)
{
h+=f[i][j];
i+=p[j]; l-=p[j];
if (l==0) break;
}
h=h%prime; if (h<0) h+=prime;
return h;
}
bool judge(int i,int j,int l)
{
int k;
for (k=len; k>=0; k--) if (p[k]<=l)
{
if (f[i][k]!=f[j][k]) return false;
l-=p[k]; i+=p[k]; j+=p[k];
if (l==0) return true;
}
}
bool exist(int l)
{
int i,r,h;
memset(hash,0,sizeof(hash));
memset(c,0,sizeof(c));
r=1;
for (i=1; i<=n-l+1; i++)
{
h=get_hash(i,l);
if (hash[h]==0)
{
hash[h]=i;
c[h]=1;
}
else {
while (hash[h]!=0 && !judge(hash[h],i,l)) h=(h+1)%prime;
if (hash[h]==0)
{
hash[h]=i;
c[h]=1;
}
else {
c[h]++;
r>?=c[h];
}
}
}
if (r>=k) return true; return false;
}
int main()
{
int i,j,l,r,m;
scanf("%d%d",&n,&k);
for (i=1; i<=n; i++) scanf("%d",&a[i]);
for (i=0; i<prime; i++)
{
j=rand()%15+1;
Rand[i]=(rand()<<j)+rand();
}
for (i=1; i<=n; i++) f[i][0]=Rand[a[i]%prime];
p[0]=1; len=0;
while (p[len]<n)
{
len++;
p[len]=p[len-1]*2;
}
for (i=1; i<=len; i++)
for (j=1; j<=n-p[i]+1; j++)
f[j][i]=f[j][i-1]*Rand[i]^f[j+p[i-1]][i-1];
l=0; r=n+1;
while (l+1<r)
{
m=(l+r)/2;
if (exist(m)) l=m;
else r=m;
}
printf("%d\n",l);
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator