字符串周期串的判读

输入:abcabcabc

输出:3

 

 class Program
    {
        static void Main(string[] args)
        {
            string str ="abcdfabcdfabcdfabcdfabcdfabcdf";
            string[] substr=new string[100];
            string substrValue;
            bool same = false;
            for (int i = 0; i < str.Length; i++)
            {
                if (str.Length % (i + 1) == 0 && i != 0)
                {
                    int j = 0;
                    int k = 0;
                    while (k + i < str.Length)
                    {
                        substr[j] = str.Substring(k, i + 1);
                        k = k + i + 1;
                        j++;
                    }
                    substrValue = substr[0];
                    for (int l = 1; l < j; l++)
                    {
                        if (substr[l] == substrValue)
                        {
                            same = true; ;
                        }
                        else
                        {
                            same = false;
                            break;
                        }
                    }

                    if (same == true)
                    {
                        Console.WriteLine("最小周期为:{0}  值为:{1}", (str.Length / (i + 1)), substrValue);
                        break;
                    }
                   
                }
               
            }

            Console.ReadKey();
        }//end Main

    }//end class

 

posted @ 2012-05-09 00:46  盈不足  阅读(320)  评论(0)    收藏  举报