Pearl necklace Beads

https://loj.ac/problem/2427

Title Description

  Gives a sequence A, seeking to make a sequence A k into k segments (not a multiple rounding the last paragraph) up to a different number of segments. And a string reversal itself same.

Thinking

  A length of this question is not large, we can enumerate violence k, count the number of different strings for each k, and then update the answer. Note that this does not say a string regardless of its order, second only to its reversal related, twice as long as we deal with the positive and negative Hash value. But this question a little card single Hash, preferably with a double Hash, but I'm too lazy to write, A tune a tune on the b.

Code

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull p=10000019;
const ull MAXN=2e5+10;
ull sum1[MAXN],sum2[MAXN],power[MAXN],ans[MAXN],n,a[MAXN];
ull f_hash(ull l,ull r,bool f)
{
    if(f)return sum1[r]-sum1[l-1]*power[r-l+1];
    else return sum2[l]-sum2[r+1]*power[r-l+1];
}
set<ull>s;
ull cal(int k)
{
    s.clear();
    for(int i=1;i+k-1<=n;i+=k)
    {
        ull tmp=min(f_hash(i,i+k-1,1),f_hash(i,i+k-1,0));
        s.insert(tmp);
    }
    return s.size();
}
int main() 
{
    scanf("%llu",&n);
    for(ull i=1;i<=n;i++)
        scanf("%llu",&a[i]);
    power[0]=1;
    for(ull i=1;i<=n;i++)
    {
        sum1[i]=sum1[i-1]*p+a[i];
        power[i]=power[i-1]*p;
    }
    for(ull i=n;i>=1;i--)
        sum2[i]=sum2[i+1]*p+a[i];
//    for(ull i=1;i<=n;i++)
//        printf("%llu %llu\n",sum1[i],sum2[i]);
    ull maxx=0,cnt=0;
    for(ull i=1;i<=n;i++)
    {
        ull now=cal(i);
        if(now>maxx)
        {
            maxx=now;
            cnt=0;
        }
        if(maxx==now)
            ans[++cnt]=i;
    }
    printf("%llu %llu\n",maxx,cnt);
    for(int i=1;i<=cnt;i++)
        printf("%llu ",ans[i]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fangbozhen/p/11620768.html