Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 如何有效地确定给定字符串中最长的单个字符回文?_Algorithm_Palindrome - Fatal编程技术网

Algorithm 如何有效地确定给定字符串中最长的单个字符回文?

Algorithm 如何有效地确定给定字符串中最长的单个字符回文?,algorithm,palindrome,Algorithm,Palindrome,给定包含字符[a-Z]的长度为N的字符串,如何确定单个字符的最长回文 我将用一个例子来说明这一点: 给定字符串:JOHNOLSON 在分析字符串时,我们发现有一个带有字符O的回文,因此字符串看起来像JOHNOLSO

给定包含字符[a-Z]的长度为N的字符串,如何确定单个字符的最长回文

我将用一个例子来说明这一点:

给定字符串:
JOHNOLSON
在分析字符串时,我们发现有一个带有字符
O
的回文,因此字符串看起来像
J
O
HN
O
LS
O
O
的回文长度为7,基本上看起来像
O
。另外,请注意,有一个回文为
N
,但长度仅为6

再比如,, 给定字符串:
ABCJOHNOLSON
给出与上面相同的结果,长度为7的
O
回文看起来像
O

但是,对于给定的字符串
ABCJOHNOLSONDA
,最长的单个字符回文长度为14,字符
A
看起来像
-
A

其他简单的例子包括:

ABA
-->
A
-
(长度3)

ABAXYZ
-->
A
-
(长度3)

ABAXYZA
-->
不是字母的回文,所以abox code>


请特别注意最后一个示例,因为它说明了问题的一个细微差别。

您可以在线性时间内完成它,这是用代码示例描述的。

以下是我想到的,我不知道它的效率有多高

For every letter in the alphabet, Find the first and last occurrence of this letter. While the substring is not a palindrome for that letter (easy to check), find the next letter on both sides so that every possible combination is checked. Take the longest one and store it. Take the longest one. 对于字母表中的每个字母, 查找此信的第一个和最后一个匹配项。 虽然子字符串不是该字母的回文(易于检查), 在两边找到下一个字母,这样每一个可能的组合都是正确的 选中的。取最长的一个保存起来。 拿最长的。
绝对不是最优的。假设输入字符串都是小写的

using System;
using System.Collections.Generic;

public class LongestPalindrome
{
    public int longest(string s)
    {
        Dictionary<char, List<int>> indices = 
            new Dictionary<char, List<int>>();

        // find all indices for each letter
        for (int i = 0; i < s.Length; i++)
        {
            char c = s[i];
            if (!indices.ContainsKey(c)) 
                    indices.Add(c, new List<int>());
            indices[c].Add(i);
        }

        int max = Int32.MinValue;

        for (int i = (int)'a'; i <= (int)'z'; i++)
        {
            char c = (char)i;

            // in case a letter didn't appear at all in the input string
            if (!indices.ContainsKey(c)) continue;

            List<int> indexList = indices[c];

            // in case a letter appeared only once in the input string
            if (indexList.Count == 1) max = Math.Max(max, 1);

            for (int j = 0; j < indexList.Count; j++)
            {
                for (int k = j + 1; k < indexList.Count; k++)
                {
                    int dist = indexList[k] - indexList[j] + 1;
                    string sub = s.Substring(indexList[j], dist);
                    if (isPalendrome(sub, c)) 
                                        max = Math.Max(max, dist);
                }   
            }
        }

        return max;
    }

    bool isPalendrome(string s, char c)
    {
        int i = 0;
        while(i < s.Length - 1 - i) 
        {
            if (s[i] != c && s[s.Length - 1 - i] != c) 
            {
                i++;
                continue;   
            }
            if (s[i] != s[s.Length - 1 - i]) return false;
            i++;
        }
        return true;
    }
}
使用系统;
使用System.Collections.Generic;
公共类最长回文
{
公共整数最长(字符串s)
{
字典索引=
新字典();
//查找每个字母的所有索引
对于(int i=0;i对于(int i=(int)'a';i当你说在两侧查找下一个字母时,你是指你当前正在搜索的字符的下一个实例吗?对于某些字符串,这将失败,例如“abaacaa”@JordanBentley:不,我的意思是(例如)尝试0,7,然后0,6,然后0,4,然后0,3,然后0,2,2,2,7,然后2,6,2,4,然后2,3,3,7,然后3,6,然后……你明白了:)但是如果在某些条件下发现回文,你可以立即打破,比如说。啊,这更有意义。如果我理解正确,那会起作用,但最糟糕的情况复杂性是O(n!)@乔丹·本特利:是的,我觉得可能没那么好。哦,好吧,这是另一种选择:)对于你要找的东西,必须有一个比“回文”更好的术语因为大多数例子不是回文。请考虑一个示例字符串<代码> AbcDaIEalMnA<代码>,在考虑<代码> > <代码>时,它看起来像<代码> A --- A—A / <代码>,这是回文(当忽略其他字符的唯一性时)。大小为12,但是考虑字符串<代码> AbcDaEEalMoNOa <代码>,其中整个字符串不再是回文,而是一个更小的子串成为最长回文,即“代码>a”——结尾的长度为5的< <代码> >。回文。我想知道是否有一种正则表达式可以解决您所寻求的问题。@blastfull我理解您所说的。您能提出一种不同的方式来思考这个问题吗?它以回文的形式呈现给我,所以我不确定它还能被描述成什么。