我有 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;
}
}
但这不应该发生在我的情况下。
如何纠正。
请建议