【问题标题】:Output of longest palindrome in a string gets printed correctly in spite of not being part of stack尽管不是堆栈的一部分,但字符串中最长回文的输出仍能正确打印
【发布时间】:2015-11-19 01:13:27
【问题描述】:

我写了一些代码来查找字符串中最长的回文(回文不必一起出现,因为它可以是不连续的)

它几乎适用于所有情况。对于下面代码中的情况,它确实会打印出正确的回文和长度。然而,有一个问题让我感到困惑。我有一个名为 compare() 的函数,我将新发现的回文长度与迄今为止的“longestPalindromeLength”进行比较,其想法是,当所有辅助函数返回主函数时,名为“longestPalindromeString”的静态(全局)变量' 会有结果。

我的问题是,当我打印它时,我在 compare() 函数的任何地方都看不到最长的回文,即“ABCDEEEEDCBA”。

请看我的代码

public class LongestPalindromeNonContiguousPrint
{
    //static String S = "abcdcba";
    //static String S = "SGEGGES";
    static String S = "SGEGGESABCDEEEEDCBA";
    //static String S = "abca1221";

    static int longestPalindromeLength = 0;
    static String longestPalindromeString = "";

    public static void main(String[] args)
    {
        System.out.println("Length of the longest palindrome == " + fun(0, S.length()-1,""));
        System.out.println("Longest palindrome == "+longestPalindromeString);
    }

    static int fun(int s, int e, String palindrome)
    {
        String temp = "";

        /* base cases for even */
        if(s == e-1)
        {
            if(S.charAt(s) == S.charAt(e))
            {
                palindrome = palindrome + S.charAt(s);
                compare(palindrome,"even");
                return 2;
            }

            else
            {
                palindrome = palindrome + S.charAt(s);
                compare(palindrome,"odd");
                return 1;
            }
        }

        /* base case for odd */
        if(s == e)
        {
            palindrome = palindrome + S.charAt(s);
            compare(palindrome,"odd");
            return 1;
        }

        /*if(s > e)
            return (S.charAt(s-1) == S.charAt(e+1)) ? 1:0;*/

        /* recurse */
        if(S.charAt(s) == S.charAt(e))
        {
            palindrome = palindrome + S.charAt(s);

            temp = palindrome;
            int rec = fun(s+1, e-1, palindrome);
            palindrome = temp;

            int ret = 2 + rec;
            return ret;
        }

        else 
        {
            temp = palindrome;
            int rec1 = fun(s+1, e, palindrome);
            palindrome = temp;

            temp = palindrome;
            int rec2 = fun(s, e-1, palindrome);
            palindrome = temp;

            return max(rec1, rec2);
        }
    }

    static int max(int a, int b)
    {
        if(a > b)
            return a;
        return b;
    }

    static void compare(String s, String type)
    {
        String palindrome = "";
        String rev = new StringBuilder(s).reverse().toString();

        if(type == "odd")
        {
            palindrome = s + rev.substring(1,rev.length());
        }

        else if(type == "even")
        {
            palindrome =  s + rev;
        }

        if(palindrome.length() > longestPalindromeLength)
        {
            longestPalindromeLength = palindrome.length();
            longestPalindromeString = palindrome;

            /* This does not get printed, I do not understand where this print() function
             * sees this string ABCDEEEEDCBA */
            if(longestPalindromeString == "ABCDEEEEDCBA")
            {
                System.out.println("found ABCDEEEEDCBA");
            }
        } 
    }
}

输出

Length of the longest palindrome == 12
Longest palindrome == ABCDEEEEDCBA

请看一下 compare() 函数,当这是最长的回文时,我插入了一个 if 条件来打印“ABCDEEEEDCBA”。但它从来没有达到这个条件。

编辑:如果输出太大,eclipse 是否会修剪掉一些输出。对于下面的程序,我观察到 eclipse 和从终端运行之间的输出差异。在 eclipse 上运行给我 24811 行输出,但是从终端运行给我 47769 输出。

public class LongestPalindromeNonContiguousPrint
{
    //static String S = "abcdcba";
    //static String S = "GEEKSFORGEEKS";
    //static String S = "SGEGGES";
    static String S = "SGEGGESABCDEEEEDCBA";
    //static String S = "abca1221";

    static int longestPalindromeLength = 0;
    static String longestPalindromeString = "";

    public static void main(String[] args)
    {
        System.out.println("Length of the longest palindrome == " + fun(0, S.length()-1,""));
        System.out.println("Longest palindrome == "+longestPalindromeString);
    }

    static int fun(int s, int e, String palindrome)
    {
        String temp = "";

        /* base cases for even */
        if(s == e-1)
        {
            if(S.charAt(s) == S.charAt(e))
            {
                palindrome = palindrome + S.charAt(s);
                compare(palindrome,"even");
                return 2;
            }

            else
            {
                palindrome = palindrome + S.charAt(s);
                compare(palindrome,"odd");
                return 1;
            }
        }

        /* base case for odd */
        if(s == e)
        {
            palindrome = palindrome + S.charAt(s);
            compare(palindrome,"odd");
            return 1;
        }

        /*if(s > e)
            return (S.charAt(s-1) == S.charAt(e+1)) ? 1:0;*/

        /* recurse */
        if(S.charAt(s) == S.charAt(e))
        {
            palindrome = palindrome + S.charAt(s);

            temp = palindrome;
            int rec = fun(s+1, e-1, palindrome);
            palindrome = temp;

            int ret = 2 + rec;
            return ret;
        }

        else 
        {
            temp = palindrome;
            int rec1 = fun(s+1, e, palindrome);
            palindrome = temp;

            temp = palindrome;
            int rec2 = fun(s, e-1, palindrome);
            palindrome = temp;

            return max(rec1, rec2);
        }
    }

    static int max(int a, int b)
    {
        if(a > b)
            return a;
        return b;
    }

    static void compare(String s, String type)
    {
        String palindrome = "";
        String rev = new StringBuilder(s).reverse().toString();

        if(type == "odd")
        {
            palindrome = s + rev.substring(1,rev.length());
        }

        else if(type == "even")
        {
            palindrome =  s + rev;
        }

        System.out.println(palindrome);

        if(palindrome.length() > longestPalindromeLength)
        {
            longestPalindromeLength = palindrome.length();
            longestPalindromeString = palindrome;

            /*if(palindrome.equals("ABCDEEEEDCBA"))
            {
                System.out.println("found ABCDEEEEDCBA");
            }*/
        } 
    }
}

【问题讨论】:

  • 请告诉我预期的输出是什么
  • == 更改为 string.equals(string)
  • @MuratK。请参阅上面的编辑。谢谢,我很困惑在 eclipse 上看到不完整的输出。
  • @SaurabhJhunjhunwala,请参阅上面的编辑。我得到了预期的输出,但只有在从终端运行时。 Eclipse 给了我不完整的输出。

标签: java recursion dynamic-programming longest-substring


【解决方案1】:

您将字符串与== 进行比较,而不是Stringequals 方法。

【讨论】:

  • 谢谢。有效。但是想问一下,如果输出太大的话,eclipse会不会剪掉一些输出。对于上述程序。请尝试打印回文并按照上面的编辑进行观察。在 eclipse 上运行给我 24811 行输出,但是从终端运行给我 47769 输出。
  • @PepperBoy 也许这可以帮助你? stackoverflow.com/questions/2828255/…
  • 谢谢,太好了。我刚刚检查了我的 eclipse 输出中的字符数,它与设置的默认限制 80000 完全相同。
猜你喜欢
  • 1970-01-01
  • 2018-12-14
  • 2022-10-01
  • 2018-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-24
  • 1970-01-01
相关资源
最近更新 更多