我写了一些代码来查找字符串中最长的回文(回文不必一起出现,因为它可以是不连续的)
它适用于几乎所有情况。对于下面代码中的情况,它确实会打印出正确的回文和长度。然而,有一个问题让我感到困惑。我有一个名为 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");
}*/
}
}
}