Java 如何使用/修改Knuth-Morris-Pratt算法将任何给定字符串转换为回文

Java 如何使用/修改Knuth-Morris-Pratt算法将任何给定字符串转换为回文,java,string,algorithm,palindrome,Java,String,Algorithm,Palindrome,我被赋予了一项任务,创建一个类,该类给定一个字符串将创建一个具有最少断言数的回文 示例运行: Input: 123333 Output: 12333321 Input: 789 Output: 78987 Input: 1221 Output: 1221221 **注意回文不应返回相同的回文 我试着使用一个修改过的KMP算法 我还原字符串并将其与反向+原始字符串进行比较,然后将不匹配添加到原始字符串中 但是,我的函数仅适用于尾随数字的输入(第一个示例输入),但是像1234这样的输入将返回1

我被赋予了一项任务,创建一个类,该类给定一个字符串将创建一个具有最少断言数的回文

示例运行:

Input: 123333
Output: 12333321

Input: 789
Output: 78987

Input: 1221
Output: 1221221
**注意回文不应返回相同的回文

我试着使用一个修改过的KMP算法

我还原字符串并将其与反向+原始字符串进行比较,然后将不匹配添加到原始字符串中

但是,我的函数仅适用于尾随数字的输入(第一个示例输入),但是像
1234
这样的输入将返回
1234123
,“92837465”将返回“928374659283746”

public static int[] computelps(String sample){
    int[] lps = new int[sample.length()];
    lps[0] = 0;
    int i = 1;
    int len = 0; // length of previous longest prefix suffix
    while (i < sample.length()) {
        if (sample.charAt(i) == sample.charAt(len)) {
            len++;
            lps[i] = len;
            i++;
        }
        else
        {
            if (len != 0) {
                len = lps[len - 1];
            }
            else {
                lps[i] = 0;
                i++;
            }
        }
    }
    return lps;
}

public static void Solution(File samplefile) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(samplefile));

    String firstline = br.readLine();
    String line;


    while ((line = br.readLine()) != null) {
        String reverse_str = "";
        String newline = line.replace(".", "");

        for (int i = newline.length() - 1; i >= 0; i--) {
            reverse_str += newline.charAt(i);
        }

        int [] lps = computelps(reverse_str); // computes the lps of the pattern string
        String tot = reverse_str + newline;

        // KMP Algorithm modified.

        int x = 0; // index for total_string(tot)
        int y = 0; // index for pattern
        String palindrome = newline;

        while (x < tot.length()){
            if(reverse_str.charAt(y) == tot.charAt(x)){
                y++;
                x++;
            }
            if(y == reverse_str.length()) {
                y = lps[y - 1];
            }


            else if( x < tot.length() && (reverse_str.charAt(y) != tot.charAt(x))){
                palindrome += tot.charAt(x);
                if ( y!= 0){
                    y = lps[y-1];
                }
                else{
                    x += 1;
                }
            }
        }
        System.out.println(palindrome);
    }
}
publicstaticint[]computelps(字符串示例){
int[]lps=新的int[sample.length()];
lps[0]=0;
int i=1;
int len=0;//前一个最长前缀后缀的长度
而(i=0;i--){
反向_str+=换行字符(i);
}
int[]lps=computelps(reverse_str);//计算模式字符串的lps
字符串tot=反向字符+换行符;
//对KMP算法进行了改进。
int x=0;//总字符串(tot)的索引
int y=0;//模式的索引
字符串回文=换行符;
而(x
我将感谢任何帮助。我发现算法非常具有挑战性,所以如果我的方法或代码低于标准,请耐心等待


*我修正了样本输入和输出,并添加了我的结果。

我想你想得太多了。问题基本上是将字符串的反向版本添加回原始版本,但不是每个字符,对吗?因此,您可能需要找到类似指针的东西来告诉函数从何处开始反转

一个例子。让字符串为12333。如果我们将索引字符串.length()中的每个字符都添加到0,那么它将是1233333321,这是不正确的,因为存在重复的3。我们需要忽略这些,因此需要将string.length()-numofDuplicateEnd中的字符添加到0

公共字符串回文(String num){
int i=数值长度()-1;
而(i>-1&&num.charAt(i)=num.charAt(num.length()-1))
我--;
对于(int k=i;k>-1;--k)
num+=num.substring(k,k+1);
返回num;
}

这有助于将此问题分解为较小的问题,为每个问题实施单独的方法,并检查每个方法是否按预期工作。真正有帮助的是学会在Ide中使用调试器。但是,在您这样做之前,您可以测试代码的每个部分是否按预期工作。因此,我简化了您的代码并将其拆分:

publicstaticvoidmain(字符串[]args){
System.out.println(“computelps”+(“[0,0,0,0]”).equals(array.toString(computelps(“4321”))?“工作”:“不工作”);
System.out.println(“反向”(“4321.equals”)?“工作”:“不工作”);
System.out.println(“解决方案”+(“1234321.equals”(解决方案(“1234”))?“工作”:“不工作”);
}
公共静态int[]computelps(字符串示例){
int[]lps=新的int[sample.length()];
lps[0]=0;
int i=1;
int len=0;//前一个最长前缀后缀的长度
而(icomputelps works
reverse works
Solution doesn't work