0

我有 2 个列表

allWords [book, pen, pencil] 
subsetString [book  pen  , book  pencil  , pen  pencil  ]

我期待我的输出为

book  pen => pencil
book  pencil => pen
pen  pencil => book

即对于subsetString 中的每个元素,我将使用allwords 对其进行检查。一旦未找到匹配项,来自 allwords 的字符串将被添加到输出的 RHS

但我的问题是现在我只得到 2 个输出而不是 3 个

allWords [book, pen, pencil]
subsetString [book  pen  , book  pencil  , pen  pencil  ]
pen  pencil   => book
book  pen   => pencil

原因是进入book pencil它时会检查allWords [book, pen, pencil]

一旦涉及到 pen contains book pencil- 它就会得到满足(因为pencil包含 substring pen)。

代码

public void getStrongRules2(ArrayList<String> allWords,String delim) {
        ArrayList<ArrayList<String>> subsets = BTSum(allWords);
        ArrayList<String> subsetString = new ArrayList<String>();

        for (int j = 0; j < subsets.size(); j++) {
            String substring = "";
            for (int k = 0; k < subsets.get(j).size(); k++) {

                substring += subsets.get(j).get(k) + "  ";
            }
            subsetString.add(substring);
        }
        System.out.println("allWords "+allWords);
        System.out.println("subsetString "+subsetString);

        for(String a : allWords){
            for (int j = 0; j < subsetString.size(); j++) {
                if (!(subsetString.get(j).contains(a))) {
                    System.out.println(subsetString.get(j)+" => "+a);
                }
            }
        }
    }
    public static ArrayList<ArrayList<String>> BTSum(ArrayList<String> numbers) {

        int n = numbers.size();
        ArrayList<ArrayList<String>> powerSet = new ArrayList<ArrayList<String>>();

        for (long i = 0; i < (1 << n); i++) {
            ArrayList<String> element = new ArrayList<String>();
            for (int j = 0; j < n; j++)
                if ((i >> j) % 2 == 1) {
                    element.add(numbers.get(j));
                }
            if (element.size() > 1 && element.size() < n) {
                powerSet.add(element);
            }
        }
        return powerSet;
    }

}

但这不应该发生在我的情况下。

如何纠正。

请建议

4

7 回答 7

1

考虑

     List<String> allWords = new ArrayList<>();
     allWords.add("Book"); allWords.add("Pen"); allWords.add("Pencil") ; 

如果您能够将您的子集拆分为三个不同的列表而不是单个数组列表,请拆分喜欢

     list1 [Book,Pen] 
     list2 [Book,Pencil]
     list3 [Pen,Pencil]

您可以在不执行循环的情况下执行此操作

     Set<String> results1 = new HashSet<String>(allWords);
     results1.removeAll(list1);
     System.out.println("result1 : "+ list1 +" ===> "+ results1);

     Set<String> results2 = new HashSet<String>(allWords);
     results2.removeAll(list2);
     System.out.println("result2 : "+ list2 +" ===> "+ results2);

     Set<String> results3 = new HashSet<String>(allWords);
     results3.removeAll(list3);
     System.out.println("result3 : "+ list3 +" ===> "+ results3);

输出

result1 : [Book, Pen] ===> [Pencil]
result2 : [Book, Pencil] ===> [Pen]
result3 : [Pen, Pencil] ===> [Book]

希望这可以帮助

于 2014-08-06T06:57:07.497 回答
0

只需按长度 desc 对子字符串进行排序,然后查找它们。使用自定义比较器,例如:

public class MyComparator implements java.util.Comparator<String> {

    private int referenceLength;

    public MyComparator(String reference) {
        super();
        this.referenceLength = reference.length();
    }

    public int compare(String s1, String s2) {
        int dist1 = Math.abs(s1.length() - referenceLength);
        int dist2 = Math.abs(s2.length() - referenceLength);

        return dist1 - dist2;
    }
}

并按它对关键字的子字符串数组进行排序

java.util.Collections.sort(List, myComparator )
于 2014-08-06T06:16:05.240 回答
0

这是因为 (subsetString.get(j).contains(a)) ->> "pen pencil".contains('pen') 是真的。因此它不会进入你的 if 循环来打印结果。

于 2014-08-06T06:24:02.233 回答
0

问题是book pencil'pen'cil containspen所以你不能使用contains().

"book pencil".contains("pen") ===> true

因此,您可以尝试以下操作

 ArrayList<String> allWords = new ArrayList<String>() {{
        add("book");
        add("pen");
        add("pencil");
    }};
 ArrayList<String> subsetString = new ArrayList<String>() {{
        add("book  pen");
        add("book  pencil");
        add("pen  pencil");
    }};

 for (String i : subsetString) {
      List<String> stringList = Arrays.asList(i.split(" "));
        for (String j : allWords) {
            if(!stringList.contains(j)) {
               System.out.println(i + " => " + j);
            }
        }
  }  
于 2014-08-06T06:27:35.593 回答
0

你不需要所有的代码。只需用于List.removeAll()完成所有繁重的工作:

List<String> allWords = Arrays.asList("book", "pen", "pencil");
List<List<String>> subsetString = Arrays.asList(
    Arrays.asList("book", "pen"), 
    Arrays.asList("book", "pencil"),
    Arrays.asList("pen", "pencil"));

for (List<String> subset : subsetString) {
    ArrayList<String> strings = new ArrayList<String>(allWords);
    strings.removeAll(subset);
    System.out.println(subset + " => " + strings);
}

输出:

[book, pen] => [pencil]
[book, pencil] => [pen]
[pen, pencil] => [book]
于 2014-08-06T06:28:39.490 回答
0

你可以这样做:

    List<String> allWords = new ArrayList<String>();
    allWords.add("book");
    allWords.add("pen");
    allWords.add("pencil");

    List<String> subsetStrring = new ArrayList<String>();
    subsetStrring.add("book pen");
    subsetStrring.add("book pencil");
    subsetStrring.add("pen pencil");

    for (String string : subsetStrring) {
        List<String> subsetStr = Arrays.asList(string.split(" "));
        for (String word : allWords) {
            if(!subsetStr.contains(word)) {
                System.out.println(string + " => " + word);
                break;
            }
        }
    }
于 2014-08-06T06:43:16.903 回答
0

我认为使用equals匹配字符串可以避免subString问题

for(String a : allWords)
{
    for (int j = 0; j < subsets.size(); j++) 
    {
        boolean isMatch = false;
        String subSetString = "";
        for(int k = 0 ; k < subsets.get(j).size() ; k++)
        {
            if(subsets.get(j).get(k).equals(a))
            {
                isMatch = true;
                break;
            }
            subSetString += subsets.get(j).get(k) + " ";
        }
        if(!isMatch) 
        {
            System.out.println(subSetString + " => " + a);
        }
    }
}
于 2014-08-06T07:02:05.607 回答