gpt4 book ai didi

java - 尽管不是堆栈的一部分,字符串中最长回文的输出仍会正确打印

转载 作者:行者123 更新时间:2023-12-01 11:12:23 26 4
gpt4 key购买 nike

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

它几乎适用于所有情况。对于下面代码中的情况,它也打印出正确的回文及其长度。然而,有一个问题让我很困惑。我有一个名为compare()的函数,其中我将新发现的回文长度与到目前为止的“最长回文长度”进行比较,其想法是,当所有辅助函数返回到主函数时,名为“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");
}*/
}
}
}

最佳答案

您正在使用 == 比较字符串,而不是使用 Stringequals 方法。

关于java - 尽管不是堆栈的一部分,字符串中最长回文的输出仍会正确打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32202196/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com