Power Strings
problem ' s Link: http://poj.org/problem?id=2406
Mean:
Gives you a string that allows you to ask how many times the string can be represented as the minimum number of loops to be repeated.
Analyse:
The use of the next array of KMP. Bare to seek the smallest follow-up link.
Time complexity:o (N)
Source code:
/*
* This code was made by Crazyacking
* verdict:accepted
* Submission date:2015-07-28-08.23
* time:0ms
* memory:137kb
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL Long Long
#define ULL unsigned long long
using namespace STD;
Const int MAXN=1000010;
CharS[MAXN];
int Next[MAXN];
void GetNext(int Len)
{
Next[0]=0;
for(int I=1,k=0;I<Len;++I)
{
while(s[I]!=S[k] && k)k=Next[k-1];
if(s[I]==S[k]) ++k;
Next[I]=k;
}
}
int Main()
{
ios_base::Sync_with_stdio(false);
Cin.Tie(0);
while(~scanf('%s ',S&&S[0]!='. ')
{
int Len=strlen(s);
GetNext(Len);
int min_cycle=Len-Next[Len-1];
if(Len%min_cycle!=0)printf("1\ n");
Else printf("%d\ n",Len/min_cycle);
}
return 0;
}
/*
*/
KMP + Minimum cycle section---POJ 2406 Power Strings