其他分享
首页 > 其他分享> > 本质串 暴力hash

本质串 暴力hash

作者:互联网

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long mm;
const int m=2e5+50;
mm l,r,t[m],a[m],b[m],zxd=10;int v[m],n,num[m],d=0,k,mmax=0;
map<pair<mm,mm>,bool>q;
inline int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
inline void write(int x){
    if(x < 0){
        putchar('-'); x = -x;
    }
    if(x > 9){
        write(x/10);
    }
    putchar(x % 10 + '0');
}
int main ()
{
    scanf("%d",&n);t[0]=1;
    for (int i = 1; i <=n ; ++i)t[i]=t[i-1]*zxd,num[i]=read();//
    for (int i = 1; i <=n ; ++i) a[i]=a[i-1]*zxd+num[i],b[n-i+1]=b[n-i+2]*zxd+num[n-i+1];
    for (int i = 1; i <=n ; ++i) { k=0;
        if (n/i<mmax)break;//即使没有相同的本质串也比最大值mmax小
        for (int j = 1; j <=n/i ; ++j) {
            l=a[j*i]-a[i*j-i]*t[i];
            r=b[i*j-i+1]-b[i*j+1]*t[i];if(l>r)swap(l,r);
            if (!q[make_pair(l,r)]){
                q[make_pair(l,r)]=1;++k;
            }
        }q.clear();
        if (k>mmax){ d=0;mmax=k;//最大值k更新 d下标更新
            v[++d]=i;
        }else  if (k==mmax)
        {
            v[++d]=i;//k相同的存数组
        }
    }printf("%d %d\n",mmax,d);
    for (int i = 1; i <=d ; ++i) {
        write(v[i]);printf(" ");
    }
}

标签:mmax,ch,hash,暴力,10,int,本质,++,long
来源: https://blog.csdn.net/m0_51060847/article/details/118735151