题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=1358
题意
给你一个字符串,求出每个前缀的最小循环节出现次数大于2的位置和次数。
思路
kmp
代码
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<queue>
#include<stack>
#include<functional>
using std::pair;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<PII,int> PPI;
typedef pair<ll,int> PLI;
const double PI=acos(-1);
const int maxn = 1e6+10;
const int maxm = 1e6 + 10;
const int mod = 1e9+7;
const int INF = 0x3f3f3f3f;
using namespace std;
int nex[maxm];
char s[maxn],t[maxm];
int m,n;
void getnext()
{
nex[0]=-1;
int j=0,k=-1;
while(j<m)
{
if(k==-1||t[j]==t[k]) nex[++j]=++k;
else k=nex[k];
}
}
int kmp()
{
getnext();
int i=0,j=0;
int ans=0;
int lens=strlen(s),lent=strlen(t);
while(i<lens)
{
if(j==-1||s[i]==t[j]) i++,j++;
else j=nex[j];
if(j==lent)
{
ans++;
//cout<<nex[j]<<endl;
j=nex[j];
}
}
return ans;
}
int main()
{
int tt=0;
while(~scanf("%d",&m)&&m)
{
tt++;
scanf("%s",t);
getnext();
printf("Test case #%d\n",tt);
for(int i=2;i<=m;i++)
{
int l=i-nex[i];
if(i%l==0&&i/l>1)
{
printf("%d %d\n",i,i/l);
}
}
printf("\n");
}
}