Period II
2024-10-07 07:42:37
For each prefix with length P of a given string S,if
S[i]=S[i+P] for i in [0..SIZE(S)-p-1],
then the prefix is a “period” of S. We want to all the periodic prefixs.
Input
Input contains multiple cases.
The first line contains an integer T representing the number of cases. Then following T cases.
Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.
Output
For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.
Sample Input
4
ooo
acmacmacmacmacma
fzufzufzuf
stostootssto
Sample Output
Case #1: 3
1 2 3
Case #2: 6
3 6 9 12 15 16
Case #3: 4
3 6 9 10
Case #4: 2
9 12
题意:求所有的循环节
思路:循环ans=ne[ans],len-ans
#include <iostream>
#include<string.h>
using namespace std;
const int maxn = 1e6+;
char a[maxn];
int ne[maxn],sum[maxn];
int main()
{
int t;
int num=;
scanf("%d",&t);
while(t--)
{
memset(ne,,sizeof(ne));
scanf("%s",a+);
int len=strlen(a+);
for(int i=,j=;i<=len;i++)
{
while(j&&a[i]!=a[j+]) j=ne[j];
if(a[i]==a[j+]) j++;
ne[i]=j;
}
int ans=len,cnt=;
while(ans)
{
ans=ne[ans];
sum[++cnt]=len-ans;
}
printf("Case #%d: %d\n",++num,cnt);
for(int i=;i<=cnt-;i++)
printf("%d ",sum[i]);
printf("%d\n",sum[cnt]);
}
return ;
}
最新文章
- Tip8:Unity中诸如 Awake() Start() Update()等函数的 执行顺序
- 9.装饰者模式(Decorator Pattern)
- 如何提取HTML代码中img的src地址?
- delphi TOpenDialog
- FMDB多线程读写问题,使用FMDataBaseQueue操作可以解决同时打开一个链接de读写问题
- git搭建服务器
- 网页标题(title)动态改变
- JS中的循环---最全的循环总结
- LR socket协议脚本
- UOJ#55. 【WC2014】紫荆花之恋 点分树 替罪羊树 平衡树 splay Treap
- UML中的六种关系
- python-浅拷贝和深拷贝
- Nginx技术研究系列5-动态路由升级版
- nginx_server_location对客户资源的辨别规则
- python 将文件描述符包装成文件对象
- css 让背景图片不停旋转
- STL - 函数作为算法的参数
- 船长带你看书——《selenium2 python 自动化测试实战》(1)
- 不能解决,复选框在request对象获取的信息后显示在用户信息里面为中文的选项名
- ffmpeg控制台上不能输出信息的解决办法