Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.66 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class hotline {
  4.     static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  5.     static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
  6.     static StringTokenizer st;
  7.     public static void main(String [] args) throws IOException{
  8.         int T = readInt();
  9.         for(int i=1; i<=T; i++){ pr.println("Dialogue #"+i+":"); go(); }
  10.         pr.close();
  11.     }
  12.     static boolean thirdSubj(String x){ return !x.equals("I") && !x.equals("you"); }
  13.     static String subjConvert(String x){
  14.         if(x.equals("I")) return "you";
  15.         else if(x.equals("you")) return "I";
  16.         return x;
  17.     }
  18.     static String [] splitVerbObj(String x){
  19.         String [] t = new String[2];
  20.         int p = x.indexOf(" ");
  21.         if(p == -1) {t[0] = x; t[1] = ""; }
  22.         else { t[0] = x.substring(0, p); t[1] = x.substring(p); }
  23.         return t;
  24.     }
  25.     static String join(String [] x, int id){
  26.         String ret = x[id];
  27.         for(int i=id+1; i<x.length; i++) ret = ret + " " + x[i];
  28.         return ret;
  29.     }
  30.     static void go()throws IOException{
  31.         Map<String, Integer> bd0=new HashMap(), bd1=new HashMap();
  32.         Map<String, Map<String, Integer> > obj= new HashMap(), do0 = new HashMap(), do1 = new HashMap();
  33.         boolean contra = false; int cnt = 0;
  34.         while(true){
  35.             String s = readLine(); int n = s.length();
  36.             if(s.charAt(n-1) == '!'){ pr.println(s); pr.println(); break; }
  37.             if(s.charAt(n-1) == '.'){ //statement
  38.                 if(contra) continue;
  39.                 s = s.substring(0, s.length()-1);
  40.                 String t[] = s.split(" "), subj1 = t[0];
  41.                 if(t[1].equals("don't") || t[1].equals("doesn't")){
  42.                     String obj1 = (t.length>2? join(t, 2):"");
  43.                     if(!do0.containsKey(subj1)) do0.put(subj1, new HashMap());
  44.                     if(!obj.containsKey(obj1)) obj.put(obj1, new HashMap());
  45.                     Map td = do0.get(t[0]), ob = obj.get(obj1);
  46.                     if(!td.containsKey(obj1)) td.put(obj1, cnt++);
  47.                     if(!ob.containsKey(subj1)) ob.put(subj1, cnt++);
  48.                     if(bd1.containsKey(obj1) || (do1.containsKey(subj1) && do1.get(subj1).containsKey(obj1)))
  49.                         contra = true;
  50.                 }else{
  51.                     String obj1 = "", obj2 = (t.length>2?" "+join(t, 2):"");
  52.                     if(thirdSubj(subj1)) obj1 = t[1].substring(0, t[1].length()-1) + obj2;
  53.                     else obj1 = t[1] + obj2;
  54.                     if(subj1.equals("nobody")){
  55.                         if(!bd0.containsKey(obj1)) bd0.put(obj1, cnt++);
  56.                         if(bd1.containsKey(obj1)) contra = true;
  57.                         for(String key: do1.keySet())
  58.                             if(do1.get(key).containsKey(obj1)) { contra = true; break; }
  59.                     }else if(subj1.equals("everybody")){
  60.                         if(!bd1.containsKey(obj1)) bd1.put(obj1, cnt++);
  61.                         if(bd0.containsKey(obj1)) contra = true;
  62.                         for(String key: do0.keySet())
  63.                             if(do0.get(key).containsKey(obj1)) { contra = true; break; }
  64.                     }else{
  65.                         if(!obj.containsKey(obj1)) obj.put(obj1, new HashMap());
  66.                         Map ob = obj.get(obj1);
  67.                         if(!obj.containsKey(subj1)) ob.put(subj1, cnt++);
  68.                         if(!do1.containsKey(subj1)) do1.put(subj1, new HashMap());
  69.                         Map td = do1.get(subj1);
  70.                         if(!td.containsKey(obj1)) td.put(obj1, cnt++);
  71.                         if(bd0.containsKey(obj1) || (do0.containsKey(subj1) && do0.get(subj1).containsKey(obj1)))
  72.                             contra = true;
  73.                     }
  74.                 }
  75.             }else if(s.charAt(n-1) == '?'){ //question
  76.                 pr.println(s); s = s.substring(0, s.length()-1);
  77.                 if(contra)  pr.println("I am abroad.");
  78.                 else{
  79.                     String t[] = s.split(" ");
  80.                     if(t[0].equals("do") || t[0].equals("does")){
  81.                         String subj1=subjConvert(t[1]);
  82.                         String obj2 =(t.length>3? " " + join(t, 3):"");
  83.                         String obj1 = t[2] + obj2;
  84.                         if(bd1.containsKey(obj1) || (do1.containsKey(t[1]) && do1.get(t[1]).containsKey(obj1)))
  85.                             pr.println("yes, "+subj1+" "+t[2]+(thirdSubj(t[1])? "s":"")+obj2+".");
  86.                         else if(bd0.containsKey(obj1)||(do0.containsKey(t[1])&&do0.get(t[1]).containsKey(obj1)))
  87.                             pr.println("no, "+subj1+" "+(thirdSubj(t[1])? "doesn't ":"don't ")+t[2]+obj2+".");
  88.                         else pr.println("maybe.");
  89.                     }else if(t[0].equals("what")){
  90.                         TreeSet<pair> objs = new TreeSet(); Map<String, Integer> tmp1 = new HashMap(bd1), tmp0=new HashMap(bd0);
  91.                         if(do0.containsKey(t[2])){
  92.                             Map<String, Integer> td = do0.get(t[2]);
  93.                             for(String e: td.keySet()){
  94.                                 if(!tmp0.containsKey(e)) tmp0.put(e, td.get(e));
  95.                                 else tmp0.put(e, Math.min(tmp0.get(e), td.get(e)));
  96.                             }
  97.                         }
  98.                         if(do1.containsKey(t[2])){
  99.                             Map<String, Integer> td = do1.get(t[2]);
  100.                             for(String e: td.keySet()){
  101.                                 if(!tmp1.containsKey(e)) tmp1.put(e, td.get(e));
  102.                                 else tmp1.put(e, Math.min(tmp1.get(e), td.get(e)));
  103.                             }
  104.                         }
  105.                         for(String e: tmp0.keySet())
  106.                             objs.add(new pair(tmp0.get(e), (thirdSubj(t[2])? "doesn't ":"don't ") + e));
  107.                         for(String e: tmp1.keySet()){
  108.                             String [] tt = splitVerbObj(e);
  109.                             objs.add(new pair(tmp1.get(e), tt[0]+(thirdSubj(t[2])? "s":"")+tt[1]));
  110.                         }
  111.                         if(objs.size() == 0) pr.println("I don't know.");
  112.                         else if(objs.size()==1) pr.println(subjConvert(t[2]) + " " + objs.first().s + ".");
  113.                         else{
  114.                             pr.print(subjConvert(t[2]) + " "); int out = 0;
  115.                             for(pair e: objs) {
  116.                                 pr.print(e.s + ", ");
  117.                                 if(++out == objs.size()-1) break;
  118.                             }
  119.                             pr.println("and " + objs.last().s+".");
  120.                         }  
  121.                     }else if(t[0].equals("who")){
  122.                         String obj1=t[1].substring(0, t[1].length()-1) + (t.length>2? " "+join(t, 2):"");
  123.                         String [] tt = splitVerbObj(obj1);
  124.                         if(bd1.containsKey(obj1)) pr.println("everybody "+tt[0]+"s"+tt[1]+".");
  125.                         else if(bd0.containsKey(obj1)) pr.println("nobody "+tt[0]+"s"+tt[1]+".");
  126.                         else{
  127.                             List<pair> tmp = new ArrayList();  Map<String, Integer> ob = obj.getOrDefault(obj1, new HashMap());
  128.                             for(String e: ob.keySet())
  129.                                 if(do1.containsKey(e) && do1.get(e).containsKey(obj1)) tmp.add(new pair(ob.get(e), e));
  130.                             if(tmp.size()==0) pr.println("I don't know.");
  131.                             else {
  132.                                 Collections.sort(tmp);
  133.                                 if(tmp.size()==1) {
  134.                                     pr.println(subjConvert(tmp.get(0).s)+" "+tt[0]+(thirdSubj(tmp.get(0).s)? "s":"")+tt[1]+".");
  135.                                 }else if(tmp.size()==2){
  136.                                     pr.println(subjConvert(tmp.get(0).s)+" and " + subjConvert(tmp.get(1).s) + " " + obj1+".");
  137.                                 }else{
  138.                                     for(int i=0; i<tmp.size()-2; i++)
  139.                                         pr.print(subjConvert(tmp.get(i).s)+", ");
  140.                                     pr.print(subjConvert(tmp.get(tmp.size()-2).s) + " ");
  141.                                     pr.println("and "+subjConvert(tmp.get(tmp.size()-1).s) + " " + obj1+".");
  142.                                 }
  143.                             }
  144.                         }
  145.                     }
  146.                 }
  147.                 pr.println();
  148.             }
  149.         }
  150.     }
  151.     static class pair implements Comparable<pair>{
  152.         public int n; public String s;
  153.         pair(int n0, String s0){ n=n0; s=s0; }
  154.         public int compareTo(pair t){ return Integer.compare(n, t.n); }
  155.     }
  156.     static String next () throws IOException {
  157.         while (st == null || !st.hasMoreTokens())
  158.             st = new StringTokenizer(br.readLine().trim());
  159.         return st.nextToken();
  160.     }
  161.     static long readLong () throws IOException {
  162.         return Long.parseLong(next());
  163.     }
  164.     static int readInt () throws IOException {
  165.         return Integer.parseInt(next());
  166.     }
  167.     static double readDouble () throws IOException {
  168.         return Double.parseDouble(next());
  169.     }
  170.     static String readLine () throws IOException {
  171.         return br.readLine().trim();
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement