C# 寻找最长可能回文的长度

C# 寻找最长可能回文的长度,c#,algorithm,palindrome,C#,Algorithm,Palindrome,我需要找到一组单词中回文的最大长度。我能够找到3-4个单词的长度,但当涉及到一些棘手的单词序列时,它失败了。我必须提取单词的第一个字符并通过检查回文的最大长度 下面是一个测试用例 using System; public class CandidateCode { public static void Main() { //BBACCABB palindrom shoud return 7 //new string[] { "Bharti

我需要找到一组单词中回文的最大长度。我能够找到3-4个单词的长度,但当涉及到一些棘手的单词序列时,它失败了。我必须提取单词的第一个字符并通过检查回文的最大长度

下面是一个测试用例

using System;
public class CandidateCode
{    
    public static void Main()
    {
        //BBACCABB palindrom shoud return 7 
        //new string[] { "Bharti", "Bharat", "Akash", "Bhavya", "Chand", "Brijesh", "Chetak", "Arvind", "Bhavna" }));
        Console.WriteLine(FindMaxLengthPalindrome(new string[] { "Bharti", "Bharat", "Akash", "Bhavya", "Chand", "Brijesh", "Chetak", "Arvind", "Bhavna" }));
        Console.ReadLine();
    }

//function to find Maximum Length Palindrom

    public static int FindMaxLengthPalindrome(string[]  input)
    {
        string s = string.Empty;
        for(int a=0;a<input.Length;a++)
        {
            s += input[a][0].ToString();
        }    
        string maxLengthPalindrome = "";    
        if (s == null) return s.Length;    
        int len = s.Length;    
        for (int i = 0; i < len; i++)
        {
            for (int j = 0; j < len - i; j++)
            {
                bool found = true;
                for (int k = j; k < (len - j) / 2; k++)
                {
                    if (s[k] != s[len - (k - j + 1)])
                    {
                        found = false;
                        break;
                    }
                }    
                if (found)
                {
                    if (len - j > maxLengthPalindrome.Length)
                        maxLengthPalindrome = s.Substring(j, len - j);
                }    
                if (maxLengthPalindrome.Length >= (len - (i + j)))
                    break;
            }    
            if (maxLengthPalindrome.Length >= (len - i))
                break;
        }    
        return maxLengthPalindrome.Length;
    }
}
使用系统;
公共类候选代码
{    
公共静态void Main()
{
//BBACCABB回文应返回7
//新字符串[]{“Bharti”、“Bharat”、“Akash”、“Bhavya”、“Chand”、“Brijesh”、“Chetak”、“Arvind”、“Bhavna”});
Console.WriteLine(FindMaxLengthPalindrome(新字符串[]{“Bharti”、“Bharat”、“Akash”、“Bhavya”、“Chand”、“Brijesh”、“Chetak”、“Arvind”、“Bhavna”);
Console.ReadLine();
}
//用于查找最大长度回文的函数
公共静态int FindMaxLengthPalindrome(字符串[]输入)
{
string s=string.Empty;
for(int a=0;a maxLengthPalindrome.Length)
maxLengthPalindrome=s.Substring(j,len-j);
}    
如果(maxLengthPalindrome.Length>=(len-(i+j)))
打破
}    
如果(maxLengthPalindrome.Length>=(len-i))
打破
}    
返回maxLengthPalindrome.Length;
}
}
通过运行上面的代码,我得到了答案6,但它应该返回7。 我在这里犯了什么错误?

试试这个:

public static int PalindromeLengthPuzzle(string[] input1)
    {

        string firstChar = string.Empty;
        foreach (string str in input1)
        {
            firstChar = firstChar + str[0];
        }
        firstChar = getLongestPalindrome(firstChar);

        return firstChar.Length;

    }


    public static String getLongestPalindrome(String input)
    {
        int rightIndex = 0, leftIndex = 0;
        String currentPalindrome = "", longestPalindrome = "";
        for (int centerIndex = 1; centerIndex < input.Length - 1; centerIndex++)
        {
            leftIndex = centerIndex - 1; rightIndex = centerIndex + 1;
            while (leftIndex == 0 && rightIndex < input.Length)
            {
                if (input[leftIndex] != input[rightIndex])
                {
                    break;
                }
                currentPalindrome = input.Substring(leftIndex, rightIndex + 1);
                longestPalindrome = currentPalindrome.Length > longestPalindrome.Length ? currentPalindrome : longestPalindrome;
                leftIndex--; rightIndex++;
            }
        }
        return longestPalindrome;
    }
公共静态int-PalindromeLengthPuzzle(字符串[]input1)
{
string firstChar=string.Empty;
foreach(input1中的字符串str)
{
firstChar=firstChar+str[0];
}
firstChar=getLongestPalindrome(firstChar);
返回firstChar.Length;
}
公共静态字符串getLongestPalindrome(字符串输入)
{
int rightIndex=0,leftIndex=0;
字符串currentPalindrome=“”,longestPalindrome=“”;
对于(int centerIndex=1;centerIndexlongestPalindrome.Length?currentPalindrome:longestPalindrome;
leftIndex--;rightIndex++;
}
}
返回最长的回文;
}

我很难弄清楚您打算让这段代码做什么。如果我看到一个名为
FindMaxLengthPalindrome
的函数,它接受一个字符串数组,我希望它能够识别这些字符串中哪一个(如果有的话)是最长的回文,并返回其长度。但这段代码根本不是这么做的。您能在此澄清您的意图吗?您的回文检查工作不正常-它标识的第一个回文是
BCBCAB
。尝试将代码拆分为更小的部分—一个用于检查回文的方法,一个用于尝试所有子字符串的方法,还有一个用于从inputs.Oops列表中提取子字符串的方法。有件事我做错了。如何纠正它。实际上,我的工作是找到给定字符串中可能最大的回文字符串。请尝试我的建议-将代码分成更小的部分,并使用调试器逐步检查其中的错误部分。您是否意识到,您的代码正在获取字符串数组中每个字符串的第一个字母,并将其连接到一个字符串中?其余的信呢?你打算这样做吗?我原以为你必须检查这些单词中是否有回文,并找出哪一个是最大的回文。