Calculate the cycle of the string 字符串周期计算

 /*
Name: Calculate the cycle of the st ring
Copyright: Personal All revised
Author: Steven 
Date: 10/07/11 20:22
Description:

                      计算因子趋近O(logN)

                      检测周期合法 O(N)

                      综上复杂度 O(NlongN)

                      可以使用KMP达到O(N)

      
*/
#include<stdlib.h>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
//get user input. return the string value
string GetUserInPut()
{
    string szInPut = "" ;
    cout<<"plase inPut"<<endl;
    cin>>szInPut;  
    return szInPut;
}
void PrintInterge(int iPrintNumber)
{
    cout<<iPrintNumber<<endl;
    system("pause");
}   
// caculate the cycle of the string,if the value is -1 which means failed(no cycle)
int GetCycle(string szOriginalStr)
{
    int iCycle = -2;
    for(int iIndexI = 0 ;iIndexI < szOriginalStr.size();iIndexI++)
    {
        if(szOriginalStr.size()  % (iIndexI + 1) == 0) // can be divied
        {
            for(int iIndexJ = iIndexI + 1; iIndexJ < szOriginalStr.size();iIndexJ++)//check whether the character left is satisfied the cycle judged
            {
                if(szOriginalStr.at(iIndexJ) == szOriginalStr.at(iIndexJ % (iIndexI + 1 )) && iIndexJ == szOriginalStr.size()-1 )// satisfied
                {
                    iCycle  =  iIndexI;
                }   
            }   
        }   
    }       
    return iCycle + 1;
}       
int main()
{
    string szDestStr = GetUserInPut();
    int iCycle = GetCycle(szDestStr);
    PrintInterge(iCycle);
}   

你可能感兴趣的:(Calculate the cycle of the string 字符串周期计算)