3

我们有一个字符串s,包含小写字母 (az)。我们可以用任何其他字符替换任何字符,并且可以多次这样做。

我们可以从 中创建一个回文字符串ps使得 中p包含给定的特定单词(即假设linkedin)。现在,我们需要找到将字符串转换sp.

ex - s=linkedininininin 那么回文字符串plinkedinnideknil,结果是 6。

第二个例子(为了更清楚) - s=linkaeiouideknil 然后p=和结果linkedinnideknil将是 4,因为我们将替换ae、和。edoun

我试图通过获取 s 和 p 的 LCS 并从 s 的长度中减去它来解决它。但问题是我如何确保回文保证包含给定的单词(Linkedin)?

请提供您的方法。谢谢。

4

3 回答 3

1

假设我正确理解了你的问题,

您可以创建回文然后替换错误的字母s

String s="linkaeiouideknil";
String p="";
String word="linkedin";
char[] wordC = word.toCharArray();
StringBuilder sb = new StringBuilder();
sb.append(word);
String drow = sb.reverse().toString();
sb.reverse();
sb.append(drow);
String pali=sb.toString();
char[] sC = s.toCharArray();
sC=Arrays.copyOf(sC, pali.length());
sb.delete(0, sb.length());
int counter=0;
for (int i = 0; i < sC.length; i++) {
    if(sC[i]!=pali.charAt(i)){
        sC[i]=pali.charAt(i);
        counter++;
    }
    sb.append(sC[i]);
}
System.out.println(counter);    
p=sb.toString();
System.out.println(p);

运行时的输出为 4。

于 2016-11-05T11:00:34.227 回答
1

我会同时迭代字符串和回文;插入输入字符串中不存在的字符,并在有下一个可用字符时替换子字符串:

public int palindromify(String pal, String read) {
    StringBuilder sb = new StringBuilder(read);
    int insertions = 0; //track insertions
    int palIndex = 0; //our progress through the palindrome
    //caches characters we know aren't further in the input, saves time
    Set<Character> none = new HashSet<>();
    boolean outOfInput = false;
    for (int i = 0;; i++) {
        if (i >= sb.length()) {
            outOfInput = true; //if we run out of input, we know we have to append remainder
            break;
        }
        if (palIndex >= pal.length()) {
            sb.delete(i, sb.length()); //remove remainder, we're out of palindrome
            break;
        }
        char curr = pal.charAt(palIndex++); //increment palindrome
        if (sb.charAt(i) != curr) {
            //perform lookahead
            boolean found = false;
            if (!none.contains(curr)) { //only search a missing char once
                for (int w = i + 1; w < sb.length(); w++) {
                    if (sb.charAt(w) == curr) {
                        sb.replace(i, w + 1, "" + curr); //replace up to our lookahead
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    none.add(curr);
                }
            }
            if (!found) {
                //simply insert our character, later ones are useful for others
                sb.insert(i, curr);
                insertions++; //this was an insertion, one of our counted values
            }
        }
    }
    //if we ran out of input, append the rest of palindrome
    return insertions + (outOfInput ? pal.length() - sb.length() : 0);
}

这为您节省了大量的复制/迭代/不必要的工作,并且应该确保最大迭代量是读入的回文长度(或输入的长度,以较短者为准)

因此在调用时:

palindromify("linkedinnideknil", "linkedinininin"); //prints '4'

创建实际的回文非常容易,而且工作量要少得多:

String s = /* some value */;
s += new StringBuilder(s).reverse();

编辑:不适用于某些边缘情况,修复。

于 2016-11-05T11:31:32.213 回答
0

我将首先创建您想要将字符串转换为的回文。接下来,计算从原始字符串到您创建的回文的编辑距离,您的编辑正在替换字符串中的字符:没有插入或删除。代码看起来像

def minReplacements(original, palindrome, m, n):
    # base case:  we're finished processing either string so we're done
    if (m == 0 or n == 0):
        return 0

    # The characters in the string match so find out how many replacements are
    # required for the remaining characters in the strings.
    if (original[m-1] == palindrome[n-1]):
        return minReplacements(origininal, palindrome, m-1, n-1)

    # Recurse on replacing a character in the original string
    # with a character in the palindrome string
    return 1 + minReplacements(origininal, palindrome, m-1, n-1)

另一方面,如果您想知道将原始字符串转换为回文字符串需要多少个字符替换、插入或删除,则使用以下代码更改上面代码的最后一行:

    return 1 + min(minReplacements(origininal, palindrome, m, n-1),   # insert character
                   minReplacements(origininal, palindrome, m-1, n-1), # replace character
                   minReplacements(origininal, palindrome, m-1, n))   # delete character

那时的代码如下所示:

def minReplacements(original, palindrome, m, n):
    # base case:  we're finished processing either string so we're done
    if (m == 0):  # done processing original string
        return n  # return the number of characters left in palindrome
    if (n == 0):  # done processing palindrome
        return m  # return the number of characters left in the original string

    # The characters in the string match so find out how many edits are
    # required for the remaining characters in the strings.
    if (original[m] == palindrome[n]):
        return minReplacements(origininal, palindrome, m-1, n-1)

    # Recurse on editing a character in the original string
    # with a character in the palindrome string
    return 1 + min(minReplacements(origininal, palindrome, m, n-1),   # insert character
                   minReplacements(origininal, palindrome, m-1, n-1), # replace character
                   minReplacements(origininal, palindrome, m-1, n))   # delete character
于 2017-05-06T15:03:29.117 回答