//============================================================================
//
// > File : poj2406.cpp
// > Author : flowertree
// > Time : 2015年12月15日
// > Algorithm : KMPnext数组的应用
//
//============================================================================
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define MAX 1000005
char str[MAX];
int Next[MAX];
int getnext(int len)
{
int j = -1;
int i = 0;
Next[0] = -1;
while(i < len)
{
if(j == -1 || str[i] == str[j])
{
++i;
++j;
Next[i] = j;
}
else
j = Next[j];
}
return Next[i];
}
int main()
{
while(scanf("%s", str) && str[0] != '.')
{
int len = strlen(str);
int temp = getnext(len);
if(len % (len - temp) == 0)
cout << len / (len - temp) << endl;
else
cout << 1 << endl;
}
system("pause");
return 0;
}
poj 2406 KMP算法Next数组的应用
最新推荐文章于 2022-04-04 20:50:21 发布