|
|
back to boardTest 5 Runtime error Posted by Pontmen 14 Sep 2016 21:38 Can somebody say whats wrong? if i use next test: 222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322222222322 5 a aa afa aaaa aafaa -1 i found right answer in Eclipse. But here have a error. my code: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; public class E1002 { public static void main(String[] args) throws IOException { String num="", strnum="", temp="",str=""; int bcnt; char n=0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); ArrayList<String> fnlstr = new ArrayList<String>(); ArrayList<String> fnl = new ArrayList<String>(); ArrayList<Integer> mass = new ArrayList<Integer>(); do { // long start = System.currentTimeMillis(); num = br.readLine(); if (!num.equals("-1")) { bcnt= Integer.parseInt(br.readLine()); String [] nmx = new String[bcnt]; String [] smx = new String[bcnt]; for (int i=0; i<bcnt;i++) { temp = br.readLine(); strnum=""; for (int i2 =0; i2<temp.length();i2++) { switch (temp.charAt(i2)) { case 'i': case 'j': n='1'; break; case 'a': case 'b': case 'c': n='2'; break; case 'd': case 'e': case 'f': n='3'; break; case 'g': case 'h': n='4'; break; case 'k': case 'l': n='5'; break; case 'm': case 'n': n='6'; break; case 'p': case 'r': case 's': n='7'; break; case 't': case 'u': case 'v': n='8'; break; case 'w': case 'x': case 'y': n='9'; break; case 'o': case 'q': case 'z': n='0'; break; } strnum=strnum+n; } nmx[i]=strnum; smx[i]=temp; } for(int i = nmx.length-1 ; i > 0 ; i--){ for(int j = 0 ; j < i ; j++){ if( nmx[j].length() < nmx[j+1].length() ){ String tmp = nmx[j]; nmx[j] = nmx[j+1]; nmx[j+1] = tmp; tmp = smx[j]; smx[j] = smx[j+1]; smx[j+1] = tmp; } } } strnum=""; String fnlst=""; int max=100,curr=0; for (int i = 0;i<nmx.length;i++) { if (num.startsWith(nmx[i], strnum.length())) { strnum=strnum+nmx[i]; curr++; str=str+smx[i]+" "; if (curr<(max)) { if (strnum.length()<num.length()) { mass.add(i); fnlstr.add(smx[i]+" "); i=-1; } if(strnum.length()==num.length()) { fnlst=str; max=curr; strnum=strnum.substring(0, strnum.length()-nmx[i].length()); str= str.substring(0, str.length()-(smx[i].length()+1)); curr--; if (i==(nmx.length-1)) { if (!mass.isEmpty()) { i=mass.get(mass.size()-1); mass.remove(mass.size()-1); strnum=strnum.substring(0, strnum.length()-(fnlstr.get(fnlstr.size()-1).length()-1)); str=str.substring(0, str.length()-fnlstr.get(fnlstr.size()-1).length()); curr--; } } } if(strnum.length()>num.length()) { strnum=strnum.substring(0, strnum.length()-nmx[i].length()); str=str.substring(0, str.length()-(smx[i].length()+1)); curr--; if (i==(nmx.length-1)) { if (!mass.isEmpty()) { i=mass.get(mass.size()-1); mass.remove(mass.size()-1); strnum=strnum.substring(0, strnum.length()-(fnlstr.get(fnlstr.size()-1).length()-1)); str=str.substring(0, str.length()-fnlstr.get(fnlstr.size()-1).length()); curr--; } } } } else { if (!mass.isEmpty()) { i=mass.get(mass.size()-1); mass.remove(mass.size()-1); strnum=strnum.substring(0, strnum.length()-(fnlstr.get(fnlstr.size()-1).length()-1)); str=str.substring(0, str.length()-fnlstr.get(fnlstr.size()-1).length()); curr--; } }
} if (i==(nmx.length-1)) { if (!mass.isEmpty()) { i=mass.get(mass.size()-1); mass.remove(mass.size()-1); strnum=""; str=""; curr--; } } } if (fnlst==""){ fnl.add("No solution."); } else fnl.add(fnlst);
} //long finish = System.currentTimeMillis(); //long timeConsumedMillis = finish - start; //System.out.println(timeConsumedMillis); } while (!num.equals("-1")); for(int i=0;i<fnl.size();i++) System.out.println(fnl.get(i)); } } |
|
|