fzu1901——循环节

题目链接:http://acm.fzu.edu.cn/problem.php?pid=1901

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

对于每个前缀,其长度为 S 的长度 P,如果‎

‎S_i_S_i_P_ i 在 [0.size(S)-p-1] 中为 i,‎

‎则前缀是 S 的"周期"。我们想要所有周期前缀。‎

‎输入‎

‎输入包含多个案例。‎

‎第一行包含表示案例数的整数 T。然后跟随 T 案例。‎

‎每个测试用例包含一个字符串 S (1 <= SIZE(S) <= 1000000),表示标题。S 由小写字母大写字母组成。‎

‎输出‎

‎对于每个测试用例,首先输出一行包含"case #x:y",其中 x 是案例编号(从 1 开始),y 是周期前缀的数量。然后按升序输出周期前缀的长度。

 

又是循环节的内容,详细资料参考我的另一篇博客:https://blog.csdn.net/qq_43472263/article/details/98782946

#include <iostream>
using namespace std;
const int maxn=1e6+7;
string s;
int nxt[maxn];
int ans[maxn];
void getnext(){
	int len=s.size();
	int j=0,k=-1;
	nxt[0]=-1;
	while(j<len){
		if(s[j]==s[k]||k==-1)
			nxt[++j]=++k;
		else k=nxt[k];
	}
}
int main(int argc, char** argv) {
	int T;
	cin>>T;
	for(int ncase=1;ncase<=T;ncase++){
		cin>>s;
		getnext();
		int len=s.size();
		int cnt=0; 
		for(int i=len;i>0;i=nxt[i])
			ans[cnt++]=len-nxt[i];
		printf("Case #%d: %d\n",ncase,cnt);
		for(int i=0;i<cnt;++i)
			printf("%d%c",ans[i],i==cnt-1?'\n':' ');
	}
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值