poj 2406 Power Strings

//题目大意:a是一个字符串,记s=a^n为a重复n次所形成的字符串。
//比如说a是abcd,那么当n=3时,a^3就是abcdabcdabcd。现在给出字符串s,求出最大的n 
#include <iostream>
#include <string>
#include <memory.h>
#include <algorithm> 
using namespace std;

int next[100000001];
void get_next(string str, int len, int *next)
{
     int i, j;
     next[0] = -1;
     for (i = 1; i <= len; i++){
         j = next[i-1]; 
         while (j > -1 && str[j] != str[i-1])
                j = next[j];
         next[i] = j + 1; 
     }
     return ; 
} 

int main()
{
    int i, len; 
    string str;
    while (cin >> str){
          if (str == ".")   break;
          len = str.length();
          memset(next, 0, sizeof(int)); 
          get_next(str, len, next); 
          if (len % (len-next[len]) == 0)
              cout << len / (len-next[len]) << endl;
          else
              cout << 1 << endl; 
    } 

    system("pause"); 
} 

你可能感兴趣的:(Algorithm,String,System)