2

我对 Levenshtein 的距离很熟悉,所以我决定用它来解决UVA 的 Edit Steps Ladder 问题。

我的解决方案是:

import java.io.*;
import java.util.*;

class LevenshteinParaElJuez implements Runnable{
    static String ReadLn(int maxLength){  // utility function to read from stdin,
                                          // Provided by Programming-challenges, edit for style only
        byte line[] = new byte [maxLength];
        int length = 0;
        int input = -1;
        try{
            while (length < maxLength){//Read untill maxlength
                input = System.in.read();
                if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
                line [length++] += input;
            }

            if ((input < 0) && (length == 0)) return null;  // eof
            return new String(line, 0, length);
         }catch (IOException e){
            return null;
        }
    }

    public static void main(String args[]) // entry point from OS
    {
        LevenshteinParaElJuez myWork = new LevenshteinParaElJuez();  // Construct the bootloader
        myWork.run();            // execute
    }

    public void run() {
        new myStuff().run();
    }
}
class myStuff implements Runnable{
    public void run(){

        ArrayList<String> theWords = new ArrayList<String>();
        try
        {

        /// PLACE YOUR JAVA CODE HERE

        String leido=LevenshteinParaElJuez.ReadLn(100);

        //System.out.println("lo leido fue "+leido);

        while (leido.length() != 0){
        theWords.add(leido);
        leido=LevenshteinParaElJuez.ReadLn(100);
        }


        }catch(Exception e){
            System.out.println("El programa genero una excepcion");
        }


        int maxEdit=0;
        int actualEdit=0;

     int wordsIndex1 =0, wordsIndex2=0;


     while (wordsIndex1<= theWords.size())
     {
      while (wordsIndex2<= theWords.size()-1){
         actualEdit=Levenshtein.computeLevenshteinDistance(theWords.get(wordsIndex1),theWords.get(wordsIndex2));
         if (actualEdit>maxEdit){maxEdit=actualEdit;}
         wordsIndex2++;
      }
     wordsIndex1++;

     }

     System.out.println(maxEdit+1);



    }


}
class Levenshtein {
    private static int minimum(int a, int b, int c) {
        if(a<=b && a<=c)
            return a;
        if(b<=a && b<=c)
            return b;
        return c;
    }

    public static int computeLevenshteinDistance(String str1, String str2) {
        return computeLevenshteinDistance(str1.toCharArray(),
                                          str2.toCharArray());
    }

    private static int computeLevenshteinDistance(char [] str1, char [] str2) {
        int [][]distance = new int[str1.length+1][str2.length+1];

        for(int i=0;i<=str1.length;i++)
                distance[i][0]=i;

        for(int j=0;j<=str2.length;j++)
            distance[0][j]=j;

        for(int i=1;i<=str1.length;i++)
            for(int j=1;j<=str2.length;j++)
                distance[i][j]= minimum(distance[i-1][j]+1,
                                        distance[i][j-1]+1,
                                        distance[i-1][j-1]+
                                        ((str1[i-1]==str2[j-1])?0:1));

        return distance[str1.length][str2.length];
    }


}

使用此输入:

cat
dig
dog
fig
fin
fine
fog
log
wine

它为此示例产生正确的输出:

5

法官拒绝了我的回答。这是我第一次尝试解决在线法官的问题,我想我可能会在这里强制一个正确的答案:

 System.out.println(maxEdit+1);

因为 maxEdit 在简单地使用 Levenshtein 计算时的值为 4。这是怎么回事?

4

2 回答 2

2

Levinshtein 是相关的,但不会为您提供输出中使用的值。在这个问题中,用它来确定两个词的编辑距离是否正好为 1,表明比较的两个词在编辑阶梯中是相邻的。

遍历字典中的单词。如果下一个单词与当前单词的编辑距离为 1,则可以将其设为当前单词,否则必须跳过。

The trick to this problem is finding all possible sequences - just because the next word has an edit distance of 1 doesn't mean using it in the ladder will give you the longest possible ladder.

于 2009-05-31T06:21:25.890 回答
1

问题是你要在字典中找到最长的字典顺序(即字母顺序)序列,这样序列中的每个单词都是通过添加、删除或更改一个字母来形成的。

所以样本结果中的 5 是序列(dig, fig, fin,fine, wine)。

我不认为 Levenshtein 与这个问题特别相关,尽管也许我的想象力不够。Levenshtein 没有捕捉到每个步骤都必须在字典中,然后在字典中的要求。

于 2009-05-30T16:21:45.383 回答