KMP + Minimum cycle section---POJ 2406 Power Strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.