Advertisement
misteraverin

Untitled

Nov 26th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.*;
  6.  
  7. /**
  8.  * Created by camp on 11/26/2014.
  9.  */
  10. public class TaskB {
  11.  
  12.     BufferedReader reader;
  13.     StringTokenizer tokenizer;
  14.     PrintWriter writer;
  15.  
  16.     public String nextToken() throws IOException {
  17.         while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  18.             tokenizer = new StringTokenizer(reader.readLine(), " \n", true);
  19.         }
  20.         return tokenizer.nextToken();
  21.     }
  22.  
  23.     public int nextInt() throws IOException {
  24.         return Integer.parseInt(nextToken());
  25.     }
  26.  
  27.     public double nextDouble() throws IOException {
  28.         return Double.parseDouble(nextToken());
  29.     }
  30.  
  31.     public long nextLong() throws IOException {
  32.         return Long.parseLong(nextToken());
  33.     }
  34.  
  35.     public static void main(String[] args) throws IOException {
  36.         new TaskB().run();
  37.     }
  38.  
  39.     public void solve() throws IOException {
  40.         int n = nextInt();
  41.         TreeMap<String, HashSet<String>> map = new TreeMap<String, HashSet<String>>(new Comparator<String>() {
  42.             @Override
  43.             public int compare(String o1, String o2) {
  44.                 return -Integer.valueOf(o1.length()).compareTo(o2.length());
  45.             }
  46.         });
  47.         boolean containsA = false;
  48.         boolean containsB = false;
  49.         boolean containsC = false;
  50.         boolean containsABC = false;
  51.         for (int i = 0; i < n; i++) {
  52.             String s1 = nextToken();
  53.  
  54.             if (s1.equals("A")) containsA = true;
  55.             if (s1.equals("B")) containsB = true;
  56.             if (s1.equals("C")) containsC = true;
  57.             if (s1.equals("ABC")) containsABC = true;
  58.  
  59.             StringBuilder s2 = new StringBuilder();
  60.             while (tokenizer.hasMoreTokens()) {
  61.                 s2.append(nextToken());
  62.             }
  63.             s2.deleteCharAt(0);
  64.             if (map.containsKey(s1)) {
  65.                 map.get(s1).add(s2.toString());
  66.             } else {
  67.                 map.put(s1, new HashSet<String>());
  68.                 map.get(s1).add(s2.toString());
  69.             }
  70.         }
  71.  
  72.         if (containsABC && containsA && containsB && containsC) {
  73.             writer.println("Impossible");
  74.             return;
  75.         }
  76.  
  77.         TreeMap<String, HashSet<String>> m2 = (TreeMap<String, HashSet<String>>) map.clone();
  78.  
  79.         ArrayDeque<String> deque = new ArrayDeque<String>();
  80.         int size = map.size();
  81.         for (int i = 0; i < size; i++) {
  82.             Map.Entry<String, HashSet<String>> entry = map.firstEntry();
  83.             String key = entry.getKey();
  84.             if (key.equals("AB")) {
  85.                 deque.addFirst(key);
  86.             } else if (key.equals("BC")) {
  87.                 deque.addLast(key);
  88.             } else if (key.equals("A")) {
  89.                 deque.addFirst(key);
  90.             } else if (key.equals("C")) {
  91.                 deque.addLast(key);
  92.             } else if (key.equals("B")) {
  93.                 if (containsA) {
  94.                     deque.addLast(key);
  95.                 } else {
  96.                     deque.addFirst(key);
  97.                 }
  98.             } else {
  99.                 deque.addFirst(key);
  100.             }
  101.             map.remove(key, entry.getValue());
  102.         }
  103.  
  104.         StringBuilder builder = new StringBuilder();
  105.         for (String key : deque) {
  106. //            System.out.println(key);
  107.             for (String s : m2.get(key)) {
  108.                 builder.append(key).append(" ").append(s).append("\n");
  109.             }
  110.         }
  111.         writer.println(builder.toString());
  112.     }
  113.  
  114.     public void run() {
  115.         try {
  116.             reader = new BufferedReader(new FileReader("b.in"));
  117.             writer = new PrintWriter("b.out");
  118.  
  119.             solve();
  120.  
  121.             writer.close();
  122.         } catch (IOException e) {
  123.             e.printStackTrace();
  124.             System.exit(1);
  125.         }
  126.     }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement