CodeForces 827 E.Rusty String(FFT)

Description

给出一个只由 V ′ V ′ , K ′ K ′ , ? ′ ? ′ 组成的字符串, ? ′ ? ′ 可以变成 V ′ V ′ K ′ K ′ ,问该字符串可能的循环节

Input

第一行输入一整数 T T 表示用例组数,每组用例首先输入一整数n表示字符串长度,之后输入一个长度为 n n 的字符串(1n5105)

Output

对于每组用例,输出该字符串可能的循环节个数和这些循环节

Sample Input

3

5
V??VK

6
??????

4
?VK?

Sample Output

2
3 5
6
1 2 3 4 5 6
3
2 3 4

Solution

对于没有问号的字符串,考虑该字符串 s s 是否可以以d为循环节,则对于 0i<nd 0 ≤ i < n − d 都有 s[i]=s[i+d] s [ i ] = s [ i + d ] ,令 ss s s s s 的反串,则有s[i]=ss[n1i],故之前的等式可以变成 s[i]=ss[n1id] s [ i ] = s s [ n − 1 − i − d ] ,用一个序列 f f 记录s V V 的位置,用另一个序列g记录 s s 的反串中V的位置,那么 fg(n1d) f ∗ g ( n − 1 − d ) 的值即表示满足 s[i]=ss[n1di]=V s [ i ] = s s [ n − 1 − d − i ] = ′ V ′ i i 个数,同理再记录K的位置可以得到满足 s[i]=ss[n1di]=K s [ i ] = s s [ n − 1 − d − i ] = ′ K ′ i i 个数,这俩加起来等于nd说明该字符串以 d d 为循环节

对于有问号的字符串,由于问号位置不确定,无法使用上面的方法去考虑,我们反向考虑,字符串s不以 d d 为循环节,用f记录 s s V的位置, g g 记录ss K K 的位置,则fg(n1d)表示满足 s[i]!=ss[n1id] s [ i ] ! = s s [ n − 1 − i − d ] 的个数,如果该值非零说明该字符串不可能以 d d 为循环节,但是注意到如果该值为0也不等价于该字符串可以以 d d 为循环节,因为会出现s[i]s[i+2d] s[i+d] s [ i + d ] 是问号的情况,但是这种情况会在判断 s s 是否以2d为循环节的时候判出来,也即如果 s s 不以d为循环节,那么 s s 不以d的所有正因子为循环节,故在卷积得到该序列后,对于不成立的循环节,给其因子也标为不成立即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
#define maxfft 524288+5
const double pi=acos(-1.0);
struct cp
{
    double a,b;
    cp operator +(const cp &o)const {return (cp){a+o.a,b+o.b};}
    cp operator -(const cp &o)const {return (cp){a-o.a,b-o.b};}
    cp operator *(const cp &o)const {return (cp){a*o.a-b*o.b,b*o.a+a*o.b};}
    cp operator *(const double &o)const {return (cp){a*o,b*o};}
    cp operator !() const{return (cp){a,-b};}
}w[maxfft];
int pos[maxfft];
void fft_init(int len)
{
    int j=0;
    while((1<<j)<len)j++;
    j--;
    for(int i=0;i<len;i++)
        pos[i]=pos[i>>1]>>1|((i&1)<<j);
}
void fft(cp *x,int len,int sta)
{
    for(int i=0;i<len;i++)
        if(i<pos[i])swap(x[i],x[pos[i]]);
    w[0]=(cp){1,0};
    for(unsigned i=2;i<=len;i<<=1)
    {
        cp g=(cp){cos(2*pi/i),sin(2*pi/i)*sta};
        for(int j=i>>1;j>=0;j-=2)w[j]=w[j>>1];
        for(int j=1;j<i>>1;j+=2)w[j]=w[j-1]*g;
        for(int j=0;j<len;j+=i)
        {
            cp *a=x+j,*b=a+(i>>1);
            for(int l=0;l<i>>1;l++)
            {
                cp o=b[l]*w[l];
                b[l]=a[l]-o;
                a[l]=a[l]+o;
            }
        }
    }
    if(sta==-1)for(int i=0;i<len;i++)x[i].a/=len,x[i].b/=len;
}

cp x[maxfft],y[maxfft],z[maxfft];
void FFT(int *a,int *b,int n,int m,int *c)
{
    int len=1;
    while(len<(n+m)>>1)len<<=1;
    fft_init(len);
    for(int i=n/2;i<len;i++)x[i].a=x[i].b=0;
    for(int i=m/2;i<len;i++)y[i].a=y[i].b=0;
    for(int i=0;i<n;i++)(i&1?x[i>>1].b:x[i>>1].a)=a[i];
    for(int i=0;i<m;i++)(i&1?y[i>>1].b:y[i>>1].a)=b[i];
    fft(x,len,1),fft(y,len,1);
    for(int i=0;i<len/2;i++)
    {
        int j=len-1&len-i;
        z[i]=x[i]*y[i]-(x[i]-!x[j])*(y[i]-!y[j])*(w[i]+(cp){1,0})*0.25;
    }
    for(int i=len/2;i<len;i++)
    {
        int j=len-1&len-i;
        z[i]=x[i]*y[i]-(x[i]-!x[j])*(y[i]-!y[j])*((cp){1,0}-w[i^len>>1])*0.25;
    }
    fft(z,len,-1);
    for(int i=0;i<n+m;i++)
        if(i&1)c[i]=(int)(z[i>>1].b+0.5);
        else c[i]=(int)(z[i>>1].a+0.5);
}
int T,n,f[2*maxfft],g[maxfft],flag[maxfft],ans[maxfft];
char s[maxfft];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)flag[i]=1;
        scanf("%s",s);
        for(int i=0;i<n;i++)
        {
            if(s[i]=='V')f[i]=1; 
            else f[i]=0;
            if(s[i]=='K')g[n-1-i]=1;
            else g[n-1-i]=0;
        }
        FFT(f,g,n,n,f);
        for(int i=1;i<n;i++)
            if(f[n-i-1]||f[n+i-1])flag[i]=0;
        for(int i=1;i<=n;i++)
            if(flag[i])
                for(int j=2*i;j<=n;j+=i)flag[i]&=flag[j];
        int res=0;
        for(int i=1;i<=n;i++)
            if(flag[i])ans[res++]=i;
        printf("%d\n",res);
        for(int i=0;i<res;i++)printf("%d%c",ans[i],i==res-1?'\n':' ');
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值