Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main {
- public static void main(String[] args) throws IOException{
- class Reaction {
- ArrayList<Integer> in = new ArrayList<Integer>(), out = new ArrayList<Integer>();
- Reaction(String s)
- {
- int number = 0, base = 1, middle = s.length()-1;
- for (int j = s.length()-1; j >= 0; j--) {
- char c = s.charAt(j);
- if (c >= '0' && c <= '9')
- {
- number += (c - '0') * base; base *= 10;
- }
- else {
- base = 1;
- out.add(number);
- number = 0;
- }
- if (c == '>')
- {
- middle = j - 2;
- break;
- }
- }
- for (int j = middle; j >= 0; j--) {
- char c = s.charAt(j);
- if (c >= '0' && c <= '9')
- {
- number += (c - '0') * base;
- base *= 10;
- }
- else {
- base = 1;
- in.add(number);
- number = 0;
- }
- if (j == 0)
- {
- in.add(number);
- break;
- }
- }
- }
- @Override
- public String toString()
- {
- return in.toString() + "->" + out.toString();
- }
- }
- Date current = new Date();
- Date start = current;
- BufferedReader bi = new BufferedReader(new InputStreamReader(System.in));
- String chemicals_string = bi.readLine();
- HashSet<Integer> chemicals = new HashSet<Integer>();
- int number = 0, base = 1;
- for (int j = chemicals_string.length()-1; j >= 0; j--) {
- char c = chemicals_string.charAt(j);
- if (c >= '0' && c <= '9')
- {
- number += (c - '0') * base; base *= 10;
- }
- else {
- base = 1;
- chemicals.add(number);
- number = 0;
- }
- if (j == 0)
- {
- chemicals.add(number);
- break;
- }
- }
- //System.out.println("Reading chemicals takes: " + (new Date().getTime() - current.getTime()) + " ms");
- ArrayList rlist = new ArrayList(100000);
- String line;
- current = new Date();
- while ((line = bi.readLine()) != null)
- {
- rlist.add( new Reaction(line));
- }
- //System.out.println("Reading react takes: " + (new Date().getTime() - current.getTime()) + " ms and ");
- //System.exit(0);
- current = new Date();
- boolean newchemicalsfound = true;
- int counter = 0;
- while (newchemicalsfound)
- {
- counter++;
- newchemicalsfound = false;
- for(Iterator<Reaction> i = rlist.iterator(); i.hasNext(); )
- {
- Reaction r = i.next();
- if (chemicals.containsAll(r.in))
- {
- chemicals.addAll(r.out);
- i.remove();
- newchemicalsfound = true;
- }
- }
- }
- //System.out.println("loop takes: " + (new Date().getTime() - current.getTime()) + " ms and " + counter + "steps");
- current = new Date();
- StringBuilder output = new StringBuilder();
- for (Integer c : chemicals)
- {
- output.append(c);
- output.append(" ");
- }
- //System.out.println("Building string takes: " + (new Date().getTime() - current.getTime()) + " ms");
- BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
- out.write(output.toString()); out.flush();
- //System.out.println(output);
- //System.out.println("Total time: " + (new Date().getTime() - start.getTime()) + " ms");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement