日期:2014-05-20 浏览次数:20775 次
import java.util.*; public class Test { public static void main(String[] args) throws Throwable { String[] a = {"0", "1"}; String[] b = {"0", "1", "2"}; String[] c = {"0", "1", "2", "3"}; String[][] src = new String[][]{a, b, c}; Map<String, List<String>> map = getRules(src); List<Map.Entry<String, List<String>>> rules = new ArrayList<Map.Entry<String, List<String>>>(map.entrySet()); Collections.sort(rules, new Comparator<Map.Entry<String, List<String>>>() { public int compare(Map.Entry<String, List<String>> r1, Map.Entry<String, List<String>> r2) { return r2.getKey().compareTo(r1.getKey()); } }); //按111(符合规则123),110(符合规则12),...000(不符合规则)的顺序,把符合规则的组合排序 String[] srcData = {"100", "101", "101", "011", "002", "020", "023", "123"}; String[] tagData = {"100", "101", "003", "012", "020", "121"}; List<String> srcList = new ArrayList<String>(Arrays.asList(srcData)); boolean match = false; List<String> rule = null; String key = null; int min = 7, last = -1; Outer: for (String s : tagData) { min = 7; last = -1; for (int i=0; i<rules.size()-1; i++) { rule = rules.get(i).getValue(); key = rules.get(i).getKey(); if (rule.contains(s)) { for (int j=0; j<srcList.size(); j++) { String ss = srcList.get(j); if (rule.contains(ss)) { match = true; for (int k=0; k<i; k++) { if (rules.get(k).getValue().contains(ss)) { match = false; if (i-k < min) { //规则不完全相同,则匹配规则最接近的 min = i-k; last = j; } break; } } if (match) { for (int k=0; k<key.length(); k++) { if (key.charAt(k) == '1') { match &= (s.charAt(k) == ss.charAt(k)); } } } if (match) { //规则完全相同的匹配 System.out.printf("%s:%s\n", ss, s); srcList.add(srcList.remove(j)); continue Outer; } else { if (last == -1) last = j; } } } } } if (last != -1) { System.out.printf("%s:%s\n", srcList.get(last), s); srcList.add(srcList.remove(last)); } } } public static Map<String, List<String>> getRules(String[][] src) { String[] rule = {"1", "01", "01"}; int[] dig = new int[src.length]; int[] bit = new int[rule.length]; StringBuilder buf = new StringBuilder(); StringBuilder key = new StringBuilder(); Map<String, List<String>> map = new HashMap<String, List<String>>(); for (int i=0; i<8; i++) { buf.delete(0, buf.length()); buf.append("000").append(Integer.toBinaryString(i)); map.put(buf.substring(buf.length()-3), new ArrayList<String>()); } while (dig[0] < src[0].length) { buf.delete(0, buf.length()); for (int i=0; i<src.length; i++) { buf.append(src[i][dig[i]]); } for (int i=0, b=i; i<8; i++, b=i) { key.delete(0, key.length()); boolean match = true; for (int j=bit.length-1; j>=0; j--) { bit[j] = b%2; b >>= 1; key.insert(0, bit[j]); if (bit[j] == 1) { match &= rule[j].contains(src[j][dig[j]]); } } if (match) { map.get(key.toString()).add(buf.toString()); } } dig[dig.length-1]++; for (int i=dig.length-1; i>0; i--) { if (dig[i] == src[i].length) { dig[i] = 0; dig[i-1]++; } else { break; } } } return map; } }