Advertisement
Kosheen

Chemical Reactions (solution) Java

Mar 4th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.25 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) throws IOException{
  7.  
  8.         class Reaction {
  9.             ArrayList<Integer> in = new ArrayList<Integer>(), out = new ArrayList<Integer>();
  10.             Reaction(String s)
  11.             {
  12.                 int number = 0, base = 1, middle = s.length()-1;
  13.                 for (int j = s.length()-1; j >= 0; j--) {
  14.                     char c = s.charAt(j);
  15.                     if (c >= '0' && c <= '9')
  16.                     {
  17.                         number += (c - '0') * base; base *= 10;
  18.                     }
  19.                     else {
  20.                         base = 1;
  21.                         out.add(number);
  22.                         number = 0;
  23.                     }
  24.                     if (c == '>')
  25.                     {
  26.                         middle = j - 2;
  27.                         break;
  28.                     }
  29.  
  30.                 }
  31.                 for (int j = middle; j >= 0; j--) {
  32.                     char c = s.charAt(j);
  33.                     if (c >= '0' && c <= '9')
  34.                     {
  35.                         number += (c - '0') * base;
  36.                         base *= 10;
  37.                     }
  38.                     else {
  39.                         base = 1;
  40.                         in.add(number);
  41.                         number = 0;
  42.                     }
  43.                     if (j == 0)
  44.                     {
  45.                         in.add(number);
  46.                         break;
  47.                     }
  48.                 }
  49.             }
  50.             @Override
  51.             public String toString()
  52.             {
  53.                 return in.toString() + "->" + out.toString();
  54.             }
  55.         }
  56.  
  57.         Date current = new Date();
  58.         Date start = current;
  59.         BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
  60.  
  61.         String chemicals_string = bi.readLine();
  62.         HashSet<Integer> chemicals = new HashSet<Integer>();
  63.  
  64.         int number = 0, base = 1;
  65.         for (int j = chemicals_string.length()-1; j >= 0; j--) {
  66.             char c = chemicals_string.charAt(j);
  67.             if (c >= '0' && c <= '9')
  68.             {
  69.                 number += (c - '0') * base; base *= 10;
  70.             }
  71.             else {
  72.                 base = 1;
  73.                 chemicals.add(number);
  74.                 number = 0;
  75.             }
  76.             if (j == 0)
  77.             {
  78.                 chemicals.add(number);
  79.                 break;
  80.             }
  81.  
  82.         }
  83.         //System.out.println("Reading chemicals takes: " + (new Date().getTime() - current.getTime()) + " ms");
  84.  
  85.         ArrayList rlist = new ArrayList(100000);
  86.         String line;
  87.         current = new Date();
  88.         while ((line = bi.readLine()) != null)
  89.         {
  90.             rlist.add( new Reaction(line));
  91.         }
  92.  
  93.         //System.out.println("Reading react takes: " + (new Date().getTime() - current.getTime()) + " ms and ");
  94.         //System.exit(0);
  95.         current = new Date();
  96.         boolean newchemicalsfound = true;
  97.         int counter = 0;
  98.         while (newchemicalsfound)
  99.         {
  100.             counter++;
  101.             newchemicalsfound = false;
  102.             for(Iterator<Reaction> i = rlist.iterator(); i.hasNext(); )
  103.             {
  104.                 Reaction r = i.next();
  105.                 if (chemicals.containsAll(r.in))
  106.                 {
  107.                     chemicals.addAll(r.out);
  108.                     i.remove();
  109.                     newchemicalsfound = true;
  110.                 }
  111.             }
  112.  
  113.         }
  114.         //System.out.println("loop takes: " + (new Date().getTime() - current.getTime()) + " ms and " + counter + "steps");
  115.         current = new Date();
  116.         StringBuilder output = new StringBuilder();
  117.         for (Integer c : chemicals)
  118.         {
  119.             output.append(c);
  120.             output.append(" ");
  121.         }
  122.         //System.out.println("Building string takes: " + (new Date().getTime() - current.getTime()) + " ms");
  123.  
  124.         BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
  125.         out.write(output.toString()); out.flush();
  126.         //System.out.println(output);
  127.         //System.out.println("Total time: " + (new Date().getTime() - start.getTime()) + " ms");
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement