2017-03-17 59 views
-1

我需要計算在列表中找到的兩個單詞的距離。而距離我指的是在源詞和目標詞之間找到的詞的數量。 ex。 dog - > cog - > cot - > cat 因此,路徑的距離爲3,因爲路徑中每個單詞之間的編輯距離爲1。但我不知道如何處理編輯距離大於1的單詞。計算兩個單詞之間的最短路徑?

+1

而你是否也有問題嗎?一個符合SO的要求? – UnholySheep

+0

堆棧溢出並不意味着爲您提供滿足您需求的免費代碼。做一些努力,並提出具體問題,以解決您遇到的問題 –

+0

我會看看這裏:http://stackoverflow.com/questions/17274183/shortest-levenshtein-distance-do-i-need-it – john16384

回答

0

這可以幫助你:

List<String> animals = new ArrayList<String>(); 
    Boolean done=false; 
    Boolean found=false; 
    int dist=0; 
    string begin="dog"; 
    string end="cat"; 

    // add 4 different values to list 
    animals.add("dog"); 
    animals.add("cog"); 
    animals.add("cot"); 
    animals.add("cat"); 
    int i = 0; 
    while (i < animals.size()&&!done) { 
     if(crunchifyList.get(i).equals(begin)) found=true; 
     if(found){ 
      dist++; 
      if(crunchifyList.get(i).equals(end))done=true; 
     } 
     i++; 
    } 
    System.out.println(dist); 
相關問題