C#实现:给定任意要给字符串,输出所有可能的回文的子字符串集合。

时间:2023-03-09 00:34:51
C#实现:给定任意要给字符串,输出所有可能的回文的子字符串集合。
class Program
{
static void Main(string[] args)
{
string testStr = "sdfadfdsfadfdsfsdf";
int length = testStr.Length;
while (length > )
{ getPalindrome(testStr.Substring(, length - ), testStr.Substring(length - ), 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 > )
{
List<string> ss = new List<string>();
ss.AddRange(palindromeStrs);
getPalindrome(subRight.Substring(, length - ), subRight.Substring(length - ), ss);
length--;
}
}
} static bool isPalindrome(string str)
{
int length = str.Length;
int half = length / ; for (int i = ; i < half; i++)
{
if (str[i] != str[length - i - ])
{
return false;
}
} return true;
}
}