C# 查找字符串中三个最长的唯一回文的算法
使用C编写一个算法来查找字符串中三个最长的唯一回文。对于三个最长的回文,按长度降序报告回文文本、起始索引和长度。例如,字符串的输出C# 查找字符串中三个最长的唯一回文的算法,c#,C#,使用C编写一个算法来查找字符串中三个最长的唯一回文。对于三个最长的回文,按长度降序报告回文文本、起始索引和长度。例如,字符串的输出 sqrrqabccbatudefggfedvwhijkllkjihxymnnmzpop 应该是: Text: hijkllkjih, Index: 23, Length: 10 Text: defggfed, Index: 13, Length: 8 Text: abccba, Index: 5 Length: 6 现在我到了可以写出回文及其长度的部分,但是
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来完成吗?或者我需要编辑其他内容吗?正如Sinatr所指出的,最理想的解决方案是在找到回文时存储回文索引 您可以改为使用函数来查找字符串中第一次出现的子字符串的索引
例如,可以在Console.WriteLine函数中使用inputString.IndexOfitem。找到回文时,需要存储更多信息 首先定义一个类:
class PalindromeResult
{
public string Text { get; set; }
public int Index { get; set; }
}
然后,创建此类的列表,而不是列表:
List<PalindromeResult> listOfPalindromes = new List<PalindromeResult>();
您必须相应地更新排序和打印。试试这个
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");
}
您可以将这两个函数混合使用以获得漂亮的代码,但我这样做是为了简化。一定要使用Regex。您必须在找到polindrome时存储有关找到polindrome的其他信息。索引将是当前的i.Regex快速指南:用str[i]替换str.Substringi,1。比较字符比比较字符串更有效。您使用inputStrArr只是为了获得长度。只需使用inputString.Length。为什么需要不同的长度?是否要在abccbadeffed中同时报告两个回文?如果要在源字符串中再次搜索,则这不是最优的。@Sinatr true,但算法无论如何都不是最优的。然后将索引也存储在列表中。创建一个保存回文和索引的结构。或使用KeyValuePair@KrisVandermotten,一个更好吗?如果同一回文在同一字符串中出现两次怎么办?您可能会为这两个事件报告相同的索引,这是错误的。或者可以使用字典而不是列表,其中int是索引,string是回文。或者使用tuple,但我自己不喜欢ItemX属性。@Sinatr-True。然而,我喜欢数据结构表达它们的意图,我觉得显式类比使用字典或元组做得更好。字典是一种通过键查找值的机制。我们这里不做这种查找。此外,对于初学者程序员来说,为此目的使用和排序词典可能会令人困惑。
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");
}