【BZOJ2081】【POI2010】—Beads(哈希)

传送门


考虑暴力枚举每个长度,用个mapmap存一下所有哈希值就可以了

BZOJBZOJ卡了bas=233bas=233311311的自然溢出哈希

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define pii pair<int,int>
#define pb push_back
#define re register
#define fi first
#define se second
#define ll long long
#define uint unsigned long long
const uint bas=3111;
const int N=200005;
uint p[N],s[N],d[N];
vector<int> ans;
int n,mx,a[N];
map<uint,bool> mp;
int main(){
	p[0]=1;
	for(int i=1;i<N;i++)p[i]=p[i-1]*bas;
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)s[i]=s[i-1]*bas+a[i];
	for(int i=n;i;i--)d[i]=d[i+1]*bas+a[i];
	for(int k=1;k<=n&&k*mx<=n;k++){
		mp.clear();
		int now=0;
		for(int i=1;i<=n;i+=k){
			if(n-i+1<k)break;
			uint res1=s[i+k-1]-s[i-1]*p[k];
			uint res2=d[i]-d[i+k]*p[k];
			if(!mp[res1]||!mp[res2])now++,mp[res1]=mp[res2]=1;
		}
		if(now>mx)mx=now,ans.clear();
		if(now==mx)ans.pb(k);
	}
	cout<<mx<<" "<<ans.size()<<'\n';
	for(int i=0;i<ans.size();i++)cout<<ans[i]<<" ";
}

posted @ 2019-07-14 22:06  Stargazer_cykoi  阅读(116)  评论(0编辑  收藏  举报