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 ...
随机推荐
- POJ 1753 (枚举+DFS)
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40632 Accepted: 17647 Descr ...
- POJ1830开关问题——gauss消元
题目链接 分析: 第一个高斯消元题目,操作是异或.奇偶能够用0.1来表示,也就表示成bool类型的方程,操作是异或.和加法没有差别 题目中有两个未知量:每一个开关被按下的次数(0.1).每一个开关的转 ...
- php跳转
header("Location: http://bbs. lampbrother.net"); header("refresh:0;url=./login.php&qu ...
- objective-c中#import和@class的差别
在Objective-C中,能够使用#import和@class来引用别的类型, 可是你知道两者有什么差别吗? @class叫做forward-class, 你常常会在头文件的定义中看到通过@cla ...
- android:PopupWindow的使用场景和注意事项
1.PopupWindow的特点 借用Google官方的说法: "A popup window that can be used to display an arbitrary view. ...
- Python数据分析简介
1,Python作为一门编程语言开发效率快,运行效率被人诟病,但是Python核心部分使用c/c++等更高效的语言来编写的还有强大的numpy, padnas, matplotlib,scipy库等应 ...
- leetcode题目解答报告(2)
Pascal's Triangle 题目描述 Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- 九度OJ 1140:八皇后 (八皇后问题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:795 解决:494 题目描述: 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * ...
- SYN FLOOD学习理解
SYN FLOOD是一种比较常见的DoS攻击手段,它的特点就是防不胜防.SYN攻击属于DOS攻击的一种,它利用TCP协议缺陷,通过发送大量的半连接请求,耗费CPU和内存资源.SYN攻击除了能影响主机外 ...
- git使用笔记(四)错误报告 Git push rejected error: fatal: refusing to merge unrelated histories
Reason: The reason is because I created repo in Github with initiated README.md file, and I tried to ...