2017-07-14 74 views
1

林試圖找出最好的方式來匹配字符串連接的結果(被稱爲userInput),其中的字符串是從其他幾個串聯的結果字符串(數組列表或陣列中的例子我稱爲批准匹配的字符串,其中字符串是從ArrayList中

ArrayList<String> approved = new ArrayList<>(); 
approved.add("abc"); 
approved.add("def"); 
approved.add("ghi"); 
approved.add("def jkl ggwp my life"); //repeated elements (abc,jkl) 
approved.add("jkl"); 
approved.add("mno"); 
approved.add("pqr"); 
approved.add("stu vwx"); 
approved.add("yz"); 

我就用此ArrayList(上圖)解釋我difficulty.But的緣故,在現實世界中,我有一個

-fixed arraylist which wont have dynamic elements (the elements in the arraylist wont change) 
    -arraylist with more than 6000 elements 
    -elements in the arraylist contains multiple word e.g ("stu vwx") 
    -repeated elements but concatenated with another string in the arraylist 

程序應返回true如果下面是userInput

userInput = "abc def"; 
userInput = "stu vwx yz"; //they can combine with another element as a whole 
userInput = "ghi"; //it doesnt have to combine with another element 
userInput = "vwx yz"; //they can split the arraylist elements with whitespace only and concatenate it with another element 

然而,該方案應返回false如果下面是userInput

userInput = "yza"; //obviously it doesnt match anything 
userInput = "ghi jk"; //doesnt match the concatenated string (ghi and jkl) 
userInput = "pqr stu v"; //it can split the element with whitespace,but it has to take the whole word 
userInput = "def abc"; //the order are important 

林想拆分userInput得到第一個字上一個字因爲順序是重要的。然後,使用包含找到它們在數組列表中的索引。

比方說

String userInput = "def ghi jkl mno"; 
//so,the firstWord and lastWord will be 
firstWord = "def"; 
lastWord = "mno"; 

從這裏,。載有()將完成其工作,並返回其有串元素的多個指數firstWord最後字(因爲firstWord在arraylist中發生多次),它將被配置爲返回另一個數組中可能的匹配。

firstWordPossibleIndex[] = {1,4}; 
lastWordPossibleIndex[] = {6}; 

由於訂單是很重要的,這裏的邏輯是,firstWordPossibleIndex應該包含比lastWordPossibleIndex較低的值,所以,如果有任何更大的價值,它可以被刪除,因爲提供的字符串會無效。

實現這個邏輯之後,它應該開始在這種情況下,從firstWordPossibleIndex下一個索引匹配的lastWordPossibleIndex

意義,它會檢查第二個字的userInput和嘗試匹配用的2和5索引的元素(因爲firstWordPossibleIndex是1和4)

它將檢查直到lastwordPossibleIndex,如果所有的單詞按照arraylist排序,它將返回true。

有了這個,我仍然遇到匹配字符串的地方,它將連接到另一個字符串的一部分。你有什麼想法解決這個問題嗎?

有沒有任何庫可以解決這個問題?

回答

0

你可以嘗試這樣的事情:

import java.util.ArrayList; 
import java.util.List; 

public class WhatSoEver{ 

    static int order; 

    public static void main(String[] args) { 

     ArrayList<String> approved = new ArrayList<>(); 
     approved.add("abc"); 
     approved.add("def"); 
     approved.add("ghi"); 
     approved.add("def jkl ggwp my life"); 
     approved.add("jkl"); 
     approved.add("mno"); 
     approved.add("pqr"); 
     approved.add("stu vwx"); 
     approved.add("yz"); 

     System.out.println(isValid(approved, "abc def")); // true 
     System.out.println(isValid(approved, "stu vwx yz")); // true 
     System.out.println(isValid(approved, "ghi")); // true 
     System.out.println(isValid(approved, "vwx yz")); // true   

     System.out.println(isValid(approved, "yza")); // false 
     System.out.println(isValid(approved, "ghi jk")); //false 
     System.out.println(isValid(approved, "pqr stu v")); //false 
     System.out.println(isValid(approved, "def abc")); //false 

    } 

    public static boolean isValid(List<String> approved, String userInput){ 
     order=0; 
     for(String word : userInput.split(" ")){ 
      if(!containsWord(approved, word)){ 
       return false; 
      } 
     } 
     return true; 
    } 

    private static boolean containsWord(List<String> approved, String word){ 
     for(int i=0; i<approved.size(); i++){ 
      for(String subS : approved.get(i).split(" ")){ 
       if(word.equals(subS) && (i+1)>order){ 
         order=i; 
         return true; 
       } 
      } 
     } 
     return false; 
    } 
} 

輸出

true 
true 
true 
true 
false 
false 
false 
false 
0

首先,您應該將每個部分(本例中爲5)分隔爲「def jlk ggwp my life」,然後將它們分別添加到列表中。然後,由空格分開的用戶的輸入,並將其保存到一個數組,只是做

approved.contains(輸入數組的元素)

如果陣列中的所有元素都存在於批准的數組列表,然後返回真正。如果其中任何一個不在批准列表中,則返回false。

0
public static void main(String[] args) { 
    ArrayList<String> approved = new ArrayList<>(); 
    approved.add("abc"); 
    approved.add("def"); 
    approved.add("ghi"); 
    approved.add("def jkl ggwp my life"); //repeated elements (abc,jkl) 
    approved.add("jkl"); 
    approved.add("mno"); 
    approved.add("pqr"); 
    approved.add("stu vwx"); 
    approved.add("yz"); 

    String[] userInput ={"abc def","stu vwx yz","ghi","vwx yz","yza","ghi jk","pqr stu v","def abc"}; 
    for(String str: userInput){ 
     System.out.println(str+"\t"+check(str,approved)); 
    } 
} 

public static boolean check(String userInput, ArrayList<String> list){ 
    String joined = String.join(" ", list)+" ";   
    return joined.contains(userInput+" "); 
}