Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 18th, 2012  |  syntax: Java  |  size: 4.05 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class SumDif {
  5.         public static void main(String[] args) throws IOException {
  6.                 Vvod qt1 = new Vvod();
  7.                 ArrayList<String> sVvod = qt1.BufR();
  8.                 System.out.println(sVvod);
  9.                 PerStr qt2 = new PerStr();
  10.                 ArrayList<String> sPer = qt2.PerArL(sVvod);
  11.                 System.out.println(sPer);
  12.                 ResStr qt4 = new ResStr();
  13.                 qt4.ResArL(sPer,sVvod);
  14.         }
  15. }
  16.  
  17. class SopNum {
  18.         public char[] chs = new char[] {
  19.                         'o', 'q', 'z',
  20.                         'i', 'j',
  21.                         'a', 'b', 'c',
  22.                         'd', 'e', 'f',
  23.                         'g', 'h',
  24.                         'k', 'l',
  25.                         'm', 'n',
  26.                         'p', 'r', 's',
  27.                         't', 'u', 'v',
  28.                         'w', 'x', 'y'};
  29.         public int[] chsn = new int[] {
  30.                         0, 0, 0,
  31.                         1, 1,
  32.                         2, 2, 2,
  33.                         3, 3, 3,
  34.                         4, 4,
  35.                         5, 5,
  36.                         6, 6,
  37.                         7, 7, 7,
  38.                         8, 8, 8,
  39.                         9, 9, 9};
  40.         public int Number(char a1) {
  41.                 int i = 0, k = -1;
  42.                 for(; i < chs.length; i++)
  43.                         if(a1 == chs[i])
  44.                                 k = chsn[i];
  45.                 if(k == -1) {
  46.                         String s = Character.toString(a1);
  47.                         return Integer.parseInt(s);
  48.                 }
  49.                 else
  50.                         return k;
  51.         }
  52. }
  53.  
  54. class Vvod {
  55.         public ArrayList<String> BufR() {
  56.                 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  57.                 ArrayList<String> al = new ArrayList<String>();
  58.                 try {
  59.                         String st;
  60.                         while((st = br.readLine()) != null) {
  61.                                 if(st.equals("-1")) break;  
  62.                         al.add(new String(st));
  63.                         }
  64.                 } catch (IOException e) {
  65.                         e.printStackTrace();
  66.                 }
  67.                 return al;
  68.         }
  69. }
  70.  
  71. class PerStr {
  72.         public ArrayList<String> PerArL(ArrayList<String> arL) {
  73.                 ArrayList<String> alr = new ArrayList<String>();       
  74.                 for(int i = 0; i < arL.size(); i++) {
  75.                         char chArs[] = new char[arL.size()];
  76.                         chArs = arL.get(i).toCharArray();
  77.                         int stInt[] = new int[chArs.length];
  78.                         for(int j = 0; j < chArs.length; j++) {
  79.                                 SopNum qt2 = new SopNum();
  80.                                 int numCh = qt2.Number(chArs[j]);
  81.                                 stInt[j] = numCh;
  82.                         }
  83.                         StringBuffer s = new StringBuffer();
  84.                         for(int j = 0; j < stInt.length; j++)                          
  85.                                 s.append(Integer.toString(stInt[j]));
  86.                         alr.add(new String(s));
  87.                 }
  88.                 return alr;
  89.         }
  90. }
  91.  
  92. class ResStr {
  93.         /*
  94.         public class MyComparator implements Comparator<String>{
  95.             @Override
  96.             public int compare(String o1, String o2) {  
  97.               if (o1.length() > o2.length()) {
  98.                  return -1;
  99.               } else if (o1.length() < o2.length()) {
  100.                  return 1;
  101.               } else {
  102.                  return o1.compareTo(o2);
  103.               }
  104.             }
  105.         }*/
  106.        
  107.         public void ResArL(ArrayList<String> arL, ArrayList<String> arL2) {
  108.                 Calculation res1 = new Calculation();
  109.                 ArrayList<String> h = new ArrayList<String>();
  110.                 ArrayList<String> h5 = new ArrayList<String>();
  111.                 int countS = arL.size();
  112.                 String beginStr;
  113.                 int i = 0, c, v, num;
  114.                 while(i < countS) {
  115.                         beginStr = arL.get(i);
  116.                         i++;
  117.                         num = Integer.parseInt(arL.get(i));
  118.                         i++;
  119.                         num += i;
  120.                         c = i; v = num;
  121.                         for(; i < num; i++)
  122.                                 h.add(arL.get(i));
  123.                         //Collections.sort(h, new MyComparator());
  124.                         h5.removeAll(h5);
  125.                         h5 = res1.Calc(0, beginStr, h, h5);
  126.                         if(h5.size() > 0) {
  127.                                 StringBuffer strRes = new StringBuffer();
  128.                                 for(int o = c; o < v; o++) {
  129.                                         for(int q = 0; q < h5.size(); q++) {
  130.                                                 if(arL.get(o).equals(h5.get(q)))
  131.                                                         if(strRes.length() > 0) {
  132.                                                                 strRes.append(" ");
  133.                                                                 strRes.append(arL2.get(o));
  134.                                                         } else {
  135.                                                                 strRes.append(arL2.get(o));
  136.                                                         }
  137.                                         }
  138.                                 }       System.out.println(strRes);
  139.                         }
  140.                         else
  141.                                 System.out.println("No Solution.");
  142.                         h.removeAll(h);
  143.                 }
  144.         }
  145. }
  146.  
  147. class Calculation {
  148.         ArrayList<String> stAr = new ArrayList<String>();
  149.         ArrayList<String> stAr2 = new ArrayList<String>();
  150.         public ArrayList<String> Calc(int position, String number, ArrayList<String> dictionary, ArrayList<String> stAr2) {
  151.                 for(int i = 0; i < dictionary.size(); i++) {
  152.                         if(position == 0) stAr.removeAll(stAr);
  153.                         if(number.indexOf(dictionary.get(i)) == position) {
  154.                                 int k = dictionary.get(i).length();
  155.                                 stAr.add(dictionary.get(i));
  156.                                 if((position+k) == number.length())
  157.                                         if(stAr2.size() < stAr.size()) {
  158.                                                 stAr2.addAll(stAr);
  159.                                         }
  160.                                 Calc(k, number, dictionary, stAr2);
  161.                                 break;
  162.                         }
  163.                 }
  164.                 return stAr2;
  165.         }
  166. }