Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.*;
- /**
- * Created by camp on 11/26/2014.
- */
- public class TaskB {
- BufferedReader reader;
- StringTokenizer tokenizer;
- PrintWriter writer;
- public String nextToken() throws IOException {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- tokenizer = new StringTokenizer(reader.readLine(), " \n", true);
- }
- return tokenizer.nextToken();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- public double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- public long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- public static void main(String[] args) throws IOException {
- new TaskB().run();
- }
- public void solve() throws IOException {
- int n = nextInt();
- TreeMap<String, HashSet<String>> map = new TreeMap<String, HashSet<String>>(new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- return -Integer.valueOf(o1.length()).compareTo(o2.length());
- }
- });
- boolean containsA = false;
- boolean containsB = false;
- boolean containsC = false;
- boolean containsABC = false;
- for (int i = 0; i < n; i++) {
- String s1 = nextToken();
- if (s1.equals("A")) containsA = true;
- if (s1.equals("B")) containsB = true;
- if (s1.equals("C")) containsC = true;
- if (s1.equals("ABC")) containsABC = true;
- StringBuilder s2 = new StringBuilder();
- while (tokenizer.hasMoreTokens()) {
- s2.append(nextToken());
- }
- s2.deleteCharAt(0);
- if (map.containsKey(s1)) {
- map.get(s1).add(s2.toString());
- } else {
- map.put(s1, new HashSet<String>());
- map.get(s1).add(s2.toString());
- }
- }
- if (containsABC && containsA && containsB && containsC) {
- writer.println("Impossible");
- return;
- }
- TreeMap<String, HashSet<String>> m2 = (TreeMap<String, HashSet<String>>) map.clone();
- ArrayDeque<String> deque = new ArrayDeque<String>();
- int size = map.size();
- for (int i = 0; i < size; i++) {
- Map.Entry<String, HashSet<String>> entry = map.firstEntry();
- String key = entry.getKey();
- if (key.equals("AB")) {
- deque.addFirst(key);
- } else if (key.equals("BC")) {
- deque.addLast(key);
- } else if (key.equals("A")) {
- deque.addFirst(key);
- } else if (key.equals("C")) {
- deque.addLast(key);
- } else if (key.equals("B")) {
- if (containsA) {
- deque.addLast(key);
- } else {
- deque.addFirst(key);
- }
- } else {
- deque.addFirst(key);
- }
- map.remove(key, entry.getValue());
- }
- StringBuilder builder = new StringBuilder();
- for (String key : deque) {
- // System.out.println(key);
- for (String s : m2.get(key)) {
- builder.append(key).append(" ").append(s).append("\n");
- }
- }
- writer.println(builder.toString());
- }
- public void run() {
- try {
- reader = new BufferedReader(new FileReader("b.in"));
- writer = new PrintWriter("b.out");
- solve();
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement