【CF827E】Rusty String

题意:给你一个01串,其中部分字符是'?',?可以是0或1,求所有可能的d,满足存在一种可能得到的01串,在向右移动d格后与自己相同。

$n\le 5\times 10^5$

题解:我们先枚举d,那么一个串符合条件当且仅当:$\forall i \in [0,d), a_i=a_{i+d}=a_{i+2d}=...$,我们可以用fft来判断是否相等。具体地,我们令a串代表正串中的V,b串代表反串中的K,求a和b的卷积c,这样只需要判断$c_d,c_{2d},c_{3d}...$是否都是0就行了。时间复杂度$O(n \log n)$。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#define pi acos(-1.0)
using namespace std;
struct cp
{
double x,y;
cp () {}
cp (double a,double b) {x=a,y=b;}
cp operator + (const cp &a) const {return cp(x+a.x,y+a.y);}
cp operator - (const cp &a) const {return cp(x-a.x,y-a.y);}
cp operator * (const cp &a) const {return cp(x*a.x-y*a.y,x*a.y+y*a.x);}
}l1[1<<20],l2[1<<20];
int n,len,top;
int v[1<<20],sta[500010];
char str[500010];
inline void FFT(cp *a,int f)
{
int i,j,k,h;
for(i=k=0;i<len;i++)
{
if(i>k) swap(a[i],a[k]);
for(j=len>>1;(k^=j)<j;j>>=1);
}
cp t;
for(h=2;h<=len;h<<=1)
{
cp wn(cos(2*pi*f/h),sin(2*pi*f/h));
for(i=0;i<len;i+=h)
{
cp w(1,0);
for(j=i;j<i+h/2;j++) t=w*a[j+h/2],a[j+h/2]=a[j]-t,a[j]=a[j]+t,w=w*wn;
}
}
}
inline void work()
{
scanf("%d%s",&n,str);
for(len=1;len<2*n;len<<=1);
int i,j;
for(i=0;i<n;i++) l1[i].x=(str[i]=='V'),l2[i].x=(str[n-i-1]=='K');
FFT(l1,1),FFT(l2,1);
for(i=0;i<len;i++) l1[i]=l1[i]*l2[i];
FFT(l1,-1);
for(i=0;i<len;i++) v[i]=int(l1[i].x/len+0.5);
for(top=0,i=1;i<=n;i++)
{
for(j=(n-1)%i;j<2*n;j+=i) if(v[j]) break;
if(j>=2*n) sta[++top]=i;
}
printf("%d\n",top);
for(i=1;i<=top;i++) printf("%d%c",sta[i],i==top?'\n':' ');
memset(l1,0,sizeof(l1[0])*(len+1)),memset(l2,0,sizeof(l2[0])*(len+1));
}
int main()
{
int T;
scanf("%d",&T);
while(T--) work();
return 0;
}//3 5 V??VK 6 ?????? 4 ?VK?

【CF827E】Rusty String 调和级数+FFT的更多相关文章

  1. 【题解】Rusty String [CF827E]

    [题解]Rusty String [CF827E] 传送门:\(\text{Rusty String}\) \(\text{[CF827E]}\) [题目描述] 多组数据,每组数据给出一个由 \(V, ...

  2. Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力

    Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consist ...

  3. E. Rusty String

    E. Rusty String time limit per test 3 seconds memory limit per test 512 megabytes input standard inp ...

  4. CF 827E Rusty String FFT

    传送门 如果没有碍事的?的话,判定字符串的循环节直接用KMP的失配数组就可以搞定.现在有了碍事的?,我们就需要考虑更通用的算法. 考虑KMP失配数组判定字符串循环节的本质,发现判定\(k\)是否为字符 ...

  5. 牛客网暑期ACM多校训练营(第三场)DEncrypted String Matching fft

    题意:给你一个解密后的字符串,给你加密方式,加密过程可能出错,字符可能加减1,然后给你一个字符串,要求匹配个数(其实我也不太懂具体怎么加密解密,反正你把给你的前两个字符串用第三个加密一下,然后搞可以有 ...

  6. Rusty String

    题意: 给定一个含有两种字符'V','K'以及?的字符串,问该串可能的循环节. 解法: 首先如果对于$d$,我们有不存在 $(j-i) | d$ 且 $S_i = 'V',  S_j = 'K'$ 的 ...

  7. FFT(快速傅立叶算法 for java)

    package com.test.test2; public class FFT {     public static final int FFT_N_LOG = 10; // FFT_N_LOG ...

  8. 【CF954I】Yet Another String Matching Problem(FFT)

    [CF954I]Yet Another String Matching Problem(FFT) 题面 给定两个字符串\(S,T\) 求\(S\)所有长度为\(|T|\)的子串与\(T\)的距离 两个 ...

  9. 2018牛客网暑假ACM多校训练赛(第三场)D Encrypted String Matching 多项式 FFT

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-D.html 题目传送门 - 2018牛客多校赛第三场 D ...

随机推荐

  1. php 区分0和空

    能够区分出来的有2,4,6 方法 public function test(){ $test=; if($test==''){ echo '<br />在php中1,0即为空'; //被输 ...

  2. Java编程思想学习笔记——访问权限修饰词

    几种访问权限修饰词 public,protected,private,friendly(Java中并无该修饰词,即包访问权限,不提供任何访问修饰词) 使用时,放置在类中成员(域或方法)的定义之前的,仅 ...

  3. iview给radio按钮组件加点击事件

    <RadioGroup v-model="formValidate.phone"> <Radio label="phone">商家电话& ...

  4. 18 如何使用go来采集windows的基本硬件信息后发送到CMDB的服务器上

    preface 之前我使用python写了cmdb采集的脚本,打包成exe的二进制文件后放在windows上执行,也达到了预期的效果. 但是最近部门要上open-falcon监控体系,每个服务器都要安 ...

  5. ubuntu14下搭建svn

    1.安装 查看是否安装 svn help 安装了卸载 sudo apt-get remove --purge subversion 安装 sudo apt-get update sudo apt-ge ...

  6. 源码分析六(org.springframework.util包之Assert类)

    一:抽象类Assert 抽象类不能够实例化对象,但是可以被继承,Assert类是功能类,所以方法都是static修饰 所以可以直接  类名.方法 调用. public abstract class A ...

  7. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

  8. UDP通信-UdpClient

    static void Main(string[] args) { Console.WriteLine("发送端"); byte[] buffer = System.Text.En ...

  9. 笔记--Wcf全面解析(上)---(1)

    using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using ...

  10. C++ 枚举转字符串

    用#宏,见代码 #include <iostream> #define enum_to_string(x) #x enum sex { boy, girl, }; int main() { ...