我为您创建了一个示例实现,它打印出您在上面指出的值列表。当然,您可以做任何您想做的事情来代替打印到控制台:
import java.util.*;
import java.lang.*;
class Main {
public static void expandPLA(char[] pla) {
// How many don't cares are we handling
int empties = 0;
for (int i = 0; i < pla.length; i++) {
if (pla[i] == '-') { empties++; }
}
// Now we know we're counting from 0 to 2^empties in binary
for (int j = 0; j < Math.pow(2,empties); j++) {
// For each value of j we're going to create a new string pattern
// and fill in each don't care with the correct digit of j
String pattern = String.copyValueOf(pla);
String bin = Integer.toBinaryString(j);
// Pad bin with zeros
int pad = empties - bin.length();
for (int z = 0; z < pad; z++) {
bin = "0" + bin;
}
// For each empty spot we're going to replace a single '-' with
// the next most significant digit
for (int k = 0; k < empties; k++) {
char digit = bin.charAt(k);
pattern = pattern.replaceFirst("-", String.valueOf(digit));
}
// We're just going to print this out for now, but you can do
// whatever it is you want at this point.
System.out.println(pattern);
}
}
public static void main (String[] args) throws java.lang.Exception {
Main.expandPLA(new char [] { '1', '-', '-', '1', '-', '1', '-', '-' });
}
}
注意:我上面的算法可能会收紧很多。我懒于如何用 0 填充我的二进制数,并且可能有比字符串替换更好的方法将我的数字放入无关空格。考虑这是一种概念证明,它可能会更节省内存和时间,但我认为它优于递归。