Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. import static java.lang.Math.*;
  5.  
  6. public class E implements Runnable {
  7.  
  8.     final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
  9.     BufferedReader in;
  10.     PrintWriter out;
  11.     StringTokenizer tok = new StringTokenizer("");
  12.  
  13.     void init() throws IOException {
  14.         if (ONLINE_JUDGE) {
  15.             in = new BufferedReader(new InputStreamReader(System.in));
  16.             out = new PrintWriter(System.out);
  17.         } else {
  18.             in = new BufferedReader(new FileReader("input.txt"));
  19.             out = new PrintWriter("output.txt");
  20.         }
  21.     }
  22.  
  23.     String readString() throws IOException {
  24.         while (!tok.hasMoreTokens()) {
  25.             tok = new StringTokenizer(in.readLine());
  26.         }
  27.         return tok.nextToken();
  28.     }
  29.  
  30.     int readInt() throws IOException {
  31.         return Integer.parseInt(readString());
  32.     }
  33.  
  34.     @Override
  35.     public void run() {
  36.         try {
  37.             long t1 = System.currentTimeMillis();
  38.             init();
  39.             solve();
  40.             out.close();
  41.             long t2 = System.currentTimeMillis();
  42.             System.err.println("Time = " + (t2 - t1));
  43.         } catch (Exception e) {
  44.             e.printStackTrace(System.err);
  45.             System.exit(-1);
  46.         }
  47.     }
  48.  
  49.     public static void main(String[] args) {
  50.         new Thread(new E()).start();
  51.     }
  52.  
  53.     String getMinimal(String s) {
  54.         char[] a = s.toCharArray();
  55.         Arrays.sort(a);
  56.         return new String(a);
  57.     }
  58.    
  59.     HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
  60.    
  61.     void inc(String min, String s) {
  62.         if (map.containsKey(min)) {
  63.             ArrayList<String> a = map.get(min);
  64.             a.add(s);
  65.         } else {
  66.             ArrayList<String> a = new ArrayList<String>();
  67.             a.add(s);
  68.             map.put(min, a);
  69.         }
  70.     }
  71.    
  72.     void solve() throws IOException {
  73.         int n = readInt();
  74.         while (n-- > 0) {
  75.             String s = readString();
  76.             String min = getMinimal(s);
  77.             inc(min, s);
  78.         }
  79.         int m = readInt();
  80.         while (m-- > 0) {
  81.             String s = readString();
  82.             String min = getMinimal(s);
  83.             if (!map.containsKey(min)) {
  84.                 out.println("No");
  85.                 continue;
  86.             }
  87.             ArrayList<String> a = map.get(min);
  88.             if (a.size() == 1) {
  89.                 out.println(a.get(0));
  90.                 continue;
  91.             }
  92.             if (a.size() > 1) {
  93.                 out.println("Multi");
  94.                 continue;
  95.             }
  96.             throw new RuntimeException();
  97.         }
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement