haizzh

博客园 首页 新随笔 联系 订阅 管理
class Program
    {
        static void Main(string[] args)
        {
            string testStr = "sdfadfdsfadfdsfsdf";
            int length = testStr.Length;
            while (length > 1)
            {

                getPalindrome(testStr.Substring(0, length - 1), testStr.Substring(length - 1), new List<string>());

                --length;
            }

            Console.ReadKey();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        static void getPalindrome(string subLeft, string subRight, List<string> leftPalindromeStr)
        {


            int length = subRight.Length;
            List<string> palindromeStrs = leftPalindromeStr;

            if (isPalindrome(subLeft) == true)
            {
                palindromeStrs.Add(subLeft);
                if (isPalindrome(subRight))
                {
                    Console.WriteLine("{0},{1}", String.Join(",", palindromeStrs), subRight);
                }

                while (length > 1)
                {
                    List<string> ss = new List<string>();
                    ss.AddRange(palindromeStrs);
                    getPalindrome(subRight.Substring(0, length - 1), subRight.Substring(length - 1), ss);
                    length--;
                }
            }
        }

        static bool isPalindrome(string str)
        {
            int length = str.Length;
            int half = length / 2;

            for (int i = 0; i < half; i++)
            {
                if (str[i] != str[length - i - 1])
                {
                    return false;
                }
            }

            return true;
        }
    }

 

posted on 2016-12-08 13:20  haizzh  阅读(214)  评论(0编辑  收藏  举报