Rusty String
题意:
给定一个含有两种字符'V','K'以及?的字符串,问该串可能的循环节。
解法:
首先如果对于$d$,我们有不存在 $(j-i) | d$ 且 $S_i = 'V', S_j = 'K'$ 的,那么 $d$ 为1循环节。
这样考虑对于每一个 $d$ 求出 $j-i = d$ 的 $S_i = 'V', S_j = 'K'$ 是否存在,然后 $O(nlogn)$ 筛一遍即可。
求 $j - i = d$ 的有
$$c_{n - i} = \sum_{0 \leq j \leq i} { a(j) b(n-i+j-1) }$$
$$c_{n - i} = (reva \otimes b)_{2n-i-1} = \sum_{0 \leq t \leq 2n-i-1}{ reva_t b_{2n-i-1-t} }$$
标准卷积,DFT即可。
#include <bits/stdc++.h> #define PI acos(-1) const int N = ; using namespace std; struct EX
{
double real,i;
EX operator+(const EX tmp)const{return (EX){real+tmp.real, i+tmp.i};};
EX operator-(const EX tmp)const{return (EX){real-tmp.real, i-tmp.i};};
EX operator*(const EX tmp)const{return (EX){real*tmp.real - i*tmp.i, real*tmp.i + i*tmp.real};};
}; int R[N<<]; void DFT(EX a[],int n,int tp_k)
{
for(int i=;i<n;i++) if(i<R[i]) swap(a[i],a[R[i]]);
for(int d=;d<n;d<<=)
{
EX wn = (EX){cos(PI/d), sin(PI/d)*tp_k};
for(int i=;i<n;i += (d<<))
{
EX wt = (EX){,};
for(int k=;k<d;k++, wt = wt*wn)
{
EX A0 = a[i+k], A1 = wt * a[i+k+d];
a[i+k] = A0+A1;
a[i+k+d] = A0-A1;
}
}
}
if(tp_k==-)
for(int i=;i<n;i++) a[i] = (EX){a[i].real/n, a[i].i/n};
} EX A[N<<],B[N<<],C[N<<];
char S[N];
int n;
bool del[N],ans[N]; void calc(char ch1,char ch2,int tot)
{
for(int i=;i<tot;i++) A[i] = B[i] = (EX){,};
for(int i=;i<n;i++) if(S[i]==ch1) B[i] = (EX){,};
for(int i=;i<=n;i++) if(S[n-i]==ch2) A[i] = (EX){,};
DFT(A,tot,);
DFT(B,tot,);
for(int i=;i<tot;i++) C[i] = A[i]*B[i];
DFT(C,tot,-);
for(int i=;i<n;i++)
{
int tmp = (int)(C[*n-i-].real+0.5);
if(tmp) del[i] = ;
}
} int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%s",&n,S);
int L = ,tot;
while((<<L)<n+n) L++;
tot = (<<L);
for(int i=;i<tot;i++) R[i]=(R[i>>]>>)|((i&)<<(L-));
for(int i=;i<n;i++) del[i] = , ans[i+] = ;
calc('V','K',tot);
calc('K','V',tot);
for(int i=n-;i>=;i--) if(!del[i]) ans[n-i-] = ;
ans[n] = ;
int cnt = ;
for(int i=;i<=n;i++)
{
for(int j=i+i;j<=n;j+=i) ans[i] &= ans[j];
if(ans[i]) cnt++;
}
printf("%d\n",cnt);
for(int i=;i<=n;i++) if(ans[i]) printf("%d ",i);
printf("\n");
}
return ;
}
Rusty String的更多相关文章
- Codeforces 827E Rusty String - 快速傅里叶变换 - 暴力
Grigory loves strings. Recently he found a metal strip on a loft. The strip had length n and consist ...
- 【CF827E】Rusty String 调和级数+FFT
[CF827E]Rusty String 题意:给你一个01串,其中部分字符是'?',?可以是0或1,求所有可能的d,满足存在一种可能得到的01串,在向右移动d格后与自己相同. $n\le 5\tim ...
- E. Rusty String
E. Rusty String time limit per test 3 seconds memory limit per test 512 megabytes input standard inp ...
- 【题解】Rusty String [CF827E]
[题解]Rusty String [CF827E] 传送门:\(\text{Rusty String}\) \(\text{[CF827E]}\) [题目描述] 多组数据,每组数据给出一个由 \(V, ...
- CF 827E Rusty String FFT
传送门 如果没有碍事的?的话,判定字符串的循环节直接用KMP的失配数组就可以搞定.现在有了碍事的?,我们就需要考虑更通用的算法. 考虑KMP失配数组判定字符串循环节的本质,发现判定\(k\)是否为字符 ...
- 透过WinDBG的视角看String
摘要 : 最近在博客园里面看到有人在讨论 C# String的一些特性. 大部分情况下是从CODING的角度来讨论String. 本人觉得非常好奇, 在运行时态, String是如何与这些特性联系上的 ...
- JavaScript String对象
本编主要介绍String 字符串对象. 目录 1. 介绍:阐述 String 对象的说明以及定义方式. 2. 实例属性:介绍 String 对象的实例属性: length. 3. 实例方法:介绍 St ...
- ElasticSearch 5学习(9)——映射和分析(string类型废弃)
在ElasticSearch中,存入文档的内容类似于传统数据每个字段一样,都会有一个指定的属性,为了能够把日期字段处理成日期,把数字字段处理成数字,把字符串字段处理成字符串值,Elasticsearc ...
- [C#] string 与 String,大 S 与小 S 之间没有什么不可言说的秘密
string 与 String,大 S 与小 S 之间没有什么不可言说的秘密 目录 小写 string 与大写 String 声明与初始化 string string 的不可变性 正则 string ...
随机推荐
- nodejs 模板字符串
范例1: for (var i=0;i<10;i++){ var data = `公司名:${i}`; console.log(data) } 输出: 实例2: var name = '丁香医生 ...
- systemd、upstart和system V
http://blog.csdn.net/kumu_linux/article/details/7653802 systemd是Linux下的一种init软件,由Lennart Poettering ...
- Git bare repo with multiple branches
http://stackoverflow.com/questions/9324762/git-bare-repo-with-multiple-branches Q: I want to make a ...
- 【转】VMware 11.0 简体中文版|附永久密钥
VMware 11.0 简体中文版|附永久密钥 昨天,VMware虚拟机11.0 简体中文版正式发布,值得注意的是新版抛弃了32位系统支持,安装包体积大幅减小, 新增Windows 10 技术预览版支 ...
- Leetcode_num2_Maximum Depth of Binary Tree
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- C# 打开指定的目录 记住路径中 / 与 \ 的使用方法
老生常谈的问题了,C#在指定目录时,路径中要使用 \\.直接看实例 using System; namespace OpenFile{ class OpenFile{ static void Main ...
- maven的介绍和安装
一.maven简介 Maven是一个项目管理和综合工具.Maven提供了开发人员构建一个完整的生命周期框架.开发团队可以自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周期. 在 ...
- Android笔记之平移View
方法有多种,只讲一种 使用View.setLeft和View.setRight 对于wrap_content的View,要横向平移,setRight是必要的,否则View的宽度会被改变(right应设 ...
- Visitor Pattern
1.Visitor模式:将更新(变更)封装到一个类中(访问操作),并由待更改类提供一个接收接口,则可在不破坏类的前提下,为类提供增加新的新操作. 2.Visitor模式结构图 Visitor模式的关键 ...
- 遇到的一个Form表单自动提交问题解决办法
Form 表单中只有一个 input 元素时按回车会默认提交表单.有的时候我们希望按回车可以进行列表查询,但是查询后表单被自动提交了,然后刷新了整个页面.这个时候就需要对这个 Form 表单处理一下以 ...