Advertisement
misteraverin

Untitled

Nov 26th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.15 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());
  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>>();
  42.         for (int i = 0; i < n; i++) {
  43.             String s1 = nextToken();
  44.             StringBuilder s2 = new StringBuilder();
  45.             while (tokenizer.hasMoreTokens()) {
  46.                 s2.append(nextToken()).append(" ");
  47.             }
  48.             s2.deleteCharAt(s2.length() - 1);
  49.             if (map.containsKey(s1)) {
  50.                 map.get(s1).add(s2.toString());
  51.             } else {
  52.                 map.put(s1, new HashSet<String>());
  53.                 map.get(s1).add(s2.toString());
  54.             }
  55.         }
  56.  
  57.         boolean[] finished = new boolean[120];
  58.         StringBuilder builder = new StringBuilder();
  59.         int s = map.size();
  60.         String lastS1 = "A";
  61.         for (int i = 0; i < s; i++) {
  62.             Map.Entry<String, HashSet<String>> e = map.firstEntry();
  63.             for (char c : lastS1.toCharArray()) {
  64.                 if (!e.getKey().contains(String.valueOf(c))) {
  65.                     finished[c] = true;
  66.                 }
  67.             }
  68.             for (char c : e.getKey().toCharArray()) {
  69.                 if (finished[c]) {
  70.                     writer.println("Impossible");
  71.                     return;
  72.                 }
  73.             }
  74. //            if (e.getKey().equals(lastS1) || (Character.valueOf(e.getKey().charAt(0)).compareTo(lastS1.charAt(lastS1.length() - 1)) >= 0)) {
  75.                 for (String s2 : e.getValue()) {
  76.  
  77.                     builder.append(e.getKey()).append(" ").append(s2).append("\n");
  78.  
  79.                 }
  80.                 lastS1 = e.getKey();
  81. //            } else {
  82. //                writer.println("Impossible");
  83. //                return;
  84. //            }
  85.             map.remove(e.getKey(), e.getValue());
  86.         }
  87.  
  88.         writer.println(builder.toString());
  89.     }
  90.  
  91.     public void run() {
  92.         try {
  93.             reader = new BufferedReader(new FileReader("b.in"));
  94.             writer = new PrintWriter("b.out");
  95.  
  96.             solve();
  97.  
  98.             writer.close();
  99.         } catch (IOException e) {
  100.             e.printStackTrace();
  101.             System.exit(1);
  102.         }
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement