**
航电oj:Period
**
#题目描述
#大意为:从第二个开始会有循环,要你找出循环的最后一个数的位置 还有循环的长度
如aaa 2 2 表示 aa 为循环体 2 为循环长度 3 3 表示为 aaa 为循环体 3 为循环长度
#运用kmp中next函数的特点 不匹配时会回到已经匹配的过的位置的后面一位
!! 这时假设前面匹配过的就是这个循环的循环体 这就对上了
位置 在i的位置时 匹配失败有 i = len, len = next【len】 —》 i-len = i - next【i】循环节长度
i/循环的长度 如果是整数 表示是循环的 输出
#注意点输出的要求 这个输出的数是不带0的 要加1
#知识点
kmp中next 数组的理解
#代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<stdio.h>
using namespace std;
char text[1000020];
char simple[1000020];
int nextt[1000020];
int n;
void find_next()
{
int sn = strlen(text);
int i = 0;
int len = -1;
nextt[0] = -1;
while(i<=sn)//! 有多一位的需求
{
if(len == -1 || text[i] == text[len])
{
len ++;
i ++;
nextt[i] = len ;
}else
{
len = nextt[len];
}
}
// for(int j=0;j<strlen(simple);j++)
// cout << nextt[j] <<endl;
}
int main()
{
int t =0;
while(scanf("%d",&n)&&n)
{
memset(text,'\0',sizeof(text));
memset(simple,'\0',sizeof(simple));
memset(nextt,0,sizeof(nextt));
scanf("%s",&text);
// cout << text[0] <<endl;
// cout << text[1] <<endl;
// cout << text[2] <<endl;
// cout << "Test case #"<<++t<<endl;
printf("Test case #%d\n",++t);
simple[0] = text[0];
simple[1] = text[1];
find_next();
for(int i=2;i<=n;i++)
{
// cout <<"i = "<< i <<endl;
//!! 太慢了 一回合要求一次 我一次求好就行了 simple变了nextt是不会变的 用text求
// simple[i] = text[i];//
// find_next();
//!!!
if(i%(i-nextt[i]) == 0)//这个是表示 0~~~i-1有循环 循环节长度为 i-nextt[i] 循环个数i/(i-nextt[i]) 但是答案要用正常的数字来输出 没有零 就要加1 变成i
{
if(i/(i-nextt[i]) > 1)//循环数要大于2
//cout << i <<" "<< i/(i-nextt[i]) << endl;
printf("%d %d\n",i,i/(i-nextt[i]));
}
}
printf("\n");
}
return 0;
}
#总结
这个题看到就应该想到kmp的next数组 因为不太可能会要你一个一个的算 从要求输出位置就可以大概看出来 要你输出的东西不是很复杂 大概就可用next数组的特性来解决 从这个方面来思考
不太懂为什么的时候看看这个大佬的讲解
https://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html