ECC Concept Rule Generation
5110 ワード
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Zuhe {
public static void main(String[] args) throws Exception {
Zuhe zhuhe = new Zuhe();
zhuhe.printAll("1,2/9,3,4/10,5/6/7/8");
}
/**
*
*/
public void printAll(String input) throws Exception {
String[] elementArray = input.split(",");
Map<String, String[]> elementAlias = new HashMap<String, String[]>();
int length = elementArray.length;
for (int i = 0; i < length; i++) {
if (elementArray[i].indexOf("/") > 0) {
String[] alias = elementArray[i].split("/");
elementArray[i] = alias[0];
String[] aliasArray = new String[alias.length - 1];
System.arraycopy(alias, 1, aliasArray, 0, alias.length - 1);
elementAlias.put(alias[0], aliasArray);
}
}
List<String[]> result = new ArrayList<String[]>();
for (int i = 2; i <= length; i++) {
result.addAll(combine(elementArray, i));
}
result = processAlias(result,elementAlias);
print(result);
}
public List<String[]> combine(String[] elements, int m) throws Exception {
int n = elements.length;
if (m > n) {
throw new Exception("m>n");
}
List<String[]> result = new ArrayList<String[]>();
int[] bs = new int[n];
for (int i = 0; i < n; i++) {
bs[i] = 0;
}
//
for (int i = 0; i < m; i++) {
bs[i] = 1;
}
if (m == n) {
result.add(print(bs, elements, m));
return result;
}
boolean flag;
boolean tempFlag;
int pos;
int sum;
// 10 , 01, 1
do {
sum = 0;
pos = 0;
tempFlag = true;
result.add(print(bs, elements, m));
for (int i = 0; i < n - 1; i++) {
if (bs[i] == 1 && bs[i + 1] == 0) {
bs[i] = 0;
bs[i + 1] = 1;
pos = i;
break;
}
}
// 1
for (int i = 0; i < pos; i++) {
if (bs[i] == 1) {
sum++;
}
}
for (int i = 0; i < pos; i++) {
if (i < sum) {
bs[i] = 1;
} else {
bs[i] = 0;
}
}
// 1
for (int i = n - m; i < n; i++) {
if (bs[i] == 0) {
tempFlag = false;
break;
}
}
flag = !tempFlag;
} while (flag);
result.add(print(bs, elements, m));
return result;
}
private String[] print(int[] bs, String[] a, int m) {
String[] result = new String[m];
int pos = 0;
for (int i = 0; i < bs.length; i++) {
if (bs[i] == 1) {
result[pos] = a[i];
pos++;
}
}
return result;
}
private void print(List<String[]> l) {
for (String[] a : l) {
for (String anA : a) {
System.out.print(anA + " ");
}
System.out.println();
}
}
private List<String[]> processAlias(List<String[]> l, Map<String, String[]> alias) {
List<String[]> result = new ArrayList<String[]>();
result.addAll(l);
for (String[] a : l) {
for (int i = 0; i < a.length; i++) {
if (alias.containsKey(a[i])) {
String[] aliasArray = alias.get(a[i]);
for (String aliasElement : aliasArray) {
String[] b = new String[a.length];
System.arraycopy(a, 0, b, 0, a.length);
b[i] = aliasElement;
result.add(b);
}
}
}
}
return result;
}
}