在字符串中查找三个最长的唯一回文的算法

本文关键字:唯一 算法 回文 三个 字符串 查找 | 更新日期: 2023-09-27 18:22:08

使用C#编写一个算法来查找字符串中最长的三个唯一回文。对于三个最长的回文,按长度降序报告回文文本、起始索引和长度。例如,字符串的输出

sqrrqabccbatudefggfedvwhijkllkjihxymnnmzpop 

应该是:

Text: hijkllkjih, Index: 23, Length: 10 Text: defggfed, Index: 13, Length: 8 Text: abccba, Index: 5 Length: 6 

现在我到了可以写出回文及其长度的部分,但索引有问题。需要关于如何包括回文索引以及如何获得唯一长度的帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputString = "sqrrqabccbatudefggfedvwhijkllkjihxymnnmzpop";
            string currentStr = string.Empty;
            List<string> listOfPalindromes = new List<string>();
            char[] inputStrArr = inputString.ToCharArray();
            for (int i = 0; i < inputStrArr.Length; i++)
            {
                for (int j = i+1; j < inputStrArr.Length; j++)
                {
                    currentStr = inputString.Substring(i, j - i + 1);
                    if (IsPalindrome(currentStr))
                    {
                        listOfPalindromes.Add(currentStr);
                    }
                }
            }
            var longest = (from str in listOfPalindromes
                           orderby str.Length descending
                           select str).Take(3);
            foreach (var item in longest)
            {
                Console.WriteLine("Text: " + item.ToString() + " Index: " +  + " Length: " + item.Length.ToString());
            }
        }
        private static bool IsPalindrome(String str)
        {
            bool IsPalindrome = true;
            if (str.Length > 0)
            {
                for (int i = 0; i < str.Length / 2; i++)
                {
                    if (str.Substring(i, 1) != str.Substring(str.Length - (i + 1), 1))
                    {
                        IsPalindrome = false;
                    }
                }
            }
            else
            {
                IsPalindrome = false;
            }
            return IsPalindrome;
        }
    }
}

好了,这就不成问题了,我该如何获得不同的长度?可以使用DISTINCT完成吗?还是需要编辑其他内容?

在字符串中查找三个最长的唯一回文的算法

当找到回文时,您需要存储更多信息。

首先定义一个类:

class PalindromeResult
{
    public string Text { get; set; }
    public int Index { get; set; }
}

然后,创建一个此类的列表,而不是List<string>

List<PalindromeResult> listOfPalindromes = new List<PalindromeResult>();

当找到结果时,像这个一样广告

if (IsPalindrome(currentStr))
{
    listOfPalindromes.Add(new PalindromeResult { Text = currentStr, Index = i});
}

您必须相应地更新排序和打印。

最理想的解决方案(如Sinatr所指出的)是在找到回文时存储它们的索引。

您可以使用IndexOf函数来查找字符串中第一个子字符串的索引。

例如,inputString.IndexOf(item)可以用于Console.WriteLine函数。

试试这个

public static bool IsPalindromic(int l)
    {
        IEnumerable<char> forwards = l.ToString().ToCharArray();
        return forwards.SequenceEqual(forwards.Reverse());
    }
    public int LongestPalindrome(List<int> integers)
    {
        int length=0;
        int num;
        foreach (var integer in integers)
        {
            if (integer.ToString().Length > length)
            {
                num = integer;
                length = integer.ToString().Length;
            }
        }
        return num;
    }

    public void  MyFunction(string input)
    {
        var numbers = Regex.Split(input, @"'D+").ToList();
        var allPalindromes = (from value in numbers where !string.IsNullOrEmpty(value) select int.Parse(value) into i where IsPalindromic(i) select i).ToList();
        if (allPalindromes.Count>0)
            Console.WriteLine(LongestPalindrome(allPalindromes));
        else
            Console.WriteLine("Any Palindrome number was found");
    }

你可以把这两个函数混合在一起,得到一个漂亮的代码,但我这样做是为了简化。