poj 2406 Power Strings

http://poj.org/problem?id=2406

题目同 poj 1961

代码:

#include<iostream>

#include<cstdio>

#include<cstring>

#include<string>

#include<cmath>

#include<queue>

#include<algorithm>

#include<set>



using namespace std;



const int N=1000010;



char s[N];

int next[N];

int ans[N];

void findnext(int n)

{

    int j=-1;

    int i=0;

    next[0]=-1;

    while(i<n)

    {

        if(j==-1||s[i]==s[j])

        {

            ++j;++i;next[i]=j;

        }else

        {

            j=next[j];

        }

    }

}

int main()

{



   int n;

   while(gets(s))

   {

       n=strlen(s);

       if(n==1&&s[0]=='.')

       break;

       findnext(n);

       int l=n-next[n];

       if(next[n]>=n/2&&n%l==0)

       {

           printf("%d\n",n/l);

       }else

       {

           printf("1\n");

       }

   }

   return 0;

}

  

你可能感兴趣的:(String)