| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有没有测试数据阿?付俺的Main.javaWA得我麻木了大家无聊的话就帮看看代码, 求bt的测试数据 我要自杀啦 ---------------------- import java.io.FileInputStream; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.Scanner; import java.util.Vector; public class Main { static class node{ public char c; public int p; public HashMap<Character,node> children; private HashMap<Integer,node[]> nchildren; public node[] input(int n){ if(nchildren.containsKey(n)){ return nchildren.get(n); }else{ char [] tmpchars=null; switch (n){ case 2: tmpchars=new char[3]; tmpchars[0]='a';tmpchars[1]='b';tmpchars[2]='c';break; case 3: tmpchars=new char[3]; tmpchars[0]='d';tmpchars[1]='e';tmpchars[2]='f';break; case 4: tmpchars=new char[3]; tmpchars[0]='g';tmpchars[1]='h';tmpchars[2]='i';break; case 5: tmpchars=new char[3]; tmpchars[0]='j';tmpchars[1]='k';tmpchars[2]='l';break; case 6: tmpchars=new char[3]; tmpchars[0]='m';tmpchars[1]='n';tmpchars[2]='o';break; case 7: tmpchars=new char[4]; tmpchars[0]='p';tmpchars[1]='q';tmpchars[2]='r';tmpchars[3]='s';break; case 8: tmpchars=new char[3]; tmpchars[0]='t';tmpchars[1]='u';tmpchars[2]='v';break; case 9: tmpchars=new char[4]; tmpchars[0]='w';tmpchars[1]='x';tmpchars[2]='y';tmpchars[3]='z';break; } return setMaxProbabilityNode(n,tmpchars); } } private static class NodeComparator implements Comparator{ public int compare(Object arg0, Object arg1) { // TODO Auto-generated method stub node p1=(node)arg0; node p2=(node)arg1; if(p1.p>p2.p) return -1; else if(p1.p==p2.p){ if(p1.c>p2.c) return 1; else if(p1.c==p2.c) return 0; return -1; } else return 1; } } private node[] setMaxProbabilityNode(int a,char [] chars){ Vector<node> tmpnodes=new Vector<node>(); for(char cc:chars){ if(children.containsKey(cc)){ tmpnodes.add(children.get(cc)); } } if(tmpnodes.size()==0)return null; node [] nodes=new node[tmpnodes.size()]; tmpnodes.toArray(nodes); Arrays.sort(nodes,new NodeComparator()); nchildren.put(a,nodes); return nodes; } public node(){ children=new HashMap<Character,node>(); nchildren=new HashMap<Integer,node[]>(); } } static node root; /** * @param args */ static int outputLength; static boolean x(String input,int inputIndex,String result,node pnode){ int in=input.charAt(inputIndex)-'0'; if(in==1){ return false; } node father[]=pnode.input(in); if(father==null||father.length==0) return true; String newResult; for(node child:father){ newResult=result+child.c; if(inputIndex==outputLength){ System.out.println(newResult); outputLength++; if(outputLength>=input.length()) return false; } if(inputIndex<input.length()-1){ if(!x(input,inputIndex+1,newResult,child))return false; } } return true; } public static void main(String[] args) throws Exception{ // TODO Auto-generated method stub Scanner in=new Scanner(System.in); //in=new Scanner(new FileInputStream("test.in")); int n; n=in.nextInt(); for(int i=0;i<n;i++){ System.out.println("Scenario #"+(i+1)+":"); int wc,ic; wc=in.nextInt(); root=null; root=new node(); for(int j=0;j<wc;j++){ String word; int probability; word=in.next(); probability=in.nextInt(); node cnode=root; for(int k=0;k<word.length();k++){ Character c=word.charAt(k); if(cnode.children.keySet().contains(c)){ cnode =cnode.children.get(c); cnode.p+=probability; continue; }else{ node nnode=new node(); nnode.p=probability; nnode.c=c; cnode.children.put(c,nnode); cnode=nnode; } } } ic=in.nextInt(); for(int j=0;j<ic;j++){ String line=in.next(); //System.out.println(line); int oneposition=line.indexOf("1"); if(oneposition<0) oneposition=line.length(); outputLength=0; x(line,0,"",root); for(int k=outputLength;k<oneposition;k++){ System.out.println("MANUALLY"); } System.out.println(); } if(i<n-1) System.out.println(); } } } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator