import java.util.*; class TNode { private Map<Integer,TNode> map = new HashMap<Integer,TNode>(); public TNode() { } public void addChild(int i,TNode t) { map.put(i, t); } public TNode getChild(int i) { return map.get(i); } } public class MakeCodes { private static int len1=7,len2=13; private TNode head = new TNode(); private static Random rand = new Random(49); public static String mkRndStr() { int len = rand.nextInt(6)+7; StringBuilder ss = new StringBuilder(); for(int i=0;i<len;i++) { ss.append(String.valueOf(rand.nextInt(9))); } return ss.toString(); } public TNode getHead() { return head; } public boolean checkQZCode(String str) { return isQZCode(head,str,0); } //i表示检验到了第几位 public boolean isQZCode(TNode t,String str,int i) { if(i==str.length()-1) { return false; } else { TNode t1 = t.getChild(str.charAt(i)-'0'); if(t1==null) { TNode t2; t1 =t; for(int j=i;j<str.length();j++) { t2 = new TNode(); t1.addChild(str.charAt(j)-'0', t2); t1 = t2; } return true; } return isQZCode(t1,str,i+1); } } public static void main(String [] args) { MakeCodes m = new MakeCodes(); int j=0; for(int i=0;i<10000;i++) { String str; do { str = mkRndStr(); j++; }while(!m.checkQZCode(str)); System.out.println(str); } System.out.println(j); } }
7-13位前缀码生成
Leave a reply