Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class hotline {
- static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- static PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
- static StringTokenizer st;
- public static void main(String [] args) throws IOException{
- int T = readInt();
- for(int i=1; i<=T; i++){ pr.println("Dialogue #"+i+":"); go(); }
- pr.close();
- }
- static boolean thirdSubj(String x){ return !x.equals("I") && !x.equals("you"); }
- static String subjConvert(String x){
- if(x.equals("I")) return "you";
- else if(x.equals("you")) return "I";
- return x;
- }
- static String [] splitVerbObj(String x){
- String [] t = new String[2];
- int p = x.indexOf(" ");
- if(p == -1) {t[0] = x; t[1] = ""; }
- else { t[0] = x.substring(0, p); t[1] = x.substring(p); }
- return t;
- }
- static String join(String [] x, int id){
- String ret = x[id];
- for(int i=id+1; i<x.length; i++) ret = ret + " " + x[i];
- return ret;
- }
- static void go()throws IOException{
- Map<String, Integer> bd0=new HashMap(), bd1=new HashMap();
- Map<String, Map<String, Integer> > obj= new HashMap(), do0 = new HashMap(), do1 = new HashMap();
- boolean contra = false; int cnt = 0;
- while(true){
- String s = readLine(); int n = s.length();
- if(s.charAt(n-1) == '!'){ pr.println(s); pr.println(); break; }
- if(s.charAt(n-1) == '.'){ //statement
- if(contra) continue;
- s = s.substring(0, s.length()-1);
- String t[] = s.split(" "), subj1 = t[0];
- if(t[1].equals("don't") || t[1].equals("doesn't")){
- String obj1 = (t.length>2? join(t, 2):"");
- if(!do0.containsKey(subj1)) do0.put(subj1, new HashMap());
- if(!obj.containsKey(obj1)) obj.put(obj1, new HashMap());
- Map td = do0.get(t[0]), ob = obj.get(obj1);
- if(!td.containsKey(obj1)) td.put(obj1, cnt++);
- if(!ob.containsKey(subj1)) ob.put(subj1, cnt++);
- if(bd1.containsKey(obj1) || (do1.containsKey(subj1) && do1.get(subj1).containsKey(obj1)))
- contra = true;
- }else{
- String obj1 = "", obj2 = (t.length>2?" "+join(t, 2):"");
- if(thirdSubj(subj1)) obj1 = t[1].substring(0, t[1].length()-1) + obj2;
- else obj1 = t[1] + obj2;
- if(subj1.equals("nobody")){
- if(!bd0.containsKey(obj1)) bd0.put(obj1, cnt++);
- if(bd1.containsKey(obj1)) contra = true;
- for(String key: do1.keySet())
- if(do1.get(key).containsKey(obj1)) { contra = true; break; }
- }else if(subj1.equals("everybody")){
- if(!bd1.containsKey(obj1)) bd1.put(obj1, cnt++);
- if(bd0.containsKey(obj1)) contra = true;
- for(String key: do0.keySet())
- if(do0.get(key).containsKey(obj1)) { contra = true; break; }
- }else{
- if(!obj.containsKey(obj1)) obj.put(obj1, new HashMap());
- Map ob = obj.get(obj1);
- if(!obj.containsKey(subj1)) ob.put(subj1, cnt++);
- if(!do1.containsKey(subj1)) do1.put(subj1, new HashMap());
- Map td = do1.get(subj1);
- if(!td.containsKey(obj1)) td.put(obj1, cnt++);
- if(bd0.containsKey(obj1) || (do0.containsKey(subj1) && do0.get(subj1).containsKey(obj1)))
- contra = true;
- }
- }
- }else if(s.charAt(n-1) == '?'){ //question
- pr.println(s); s = s.substring(0, s.length()-1);
- if(contra) pr.println("I am abroad.");
- else{
- String t[] = s.split(" ");
- if(t[0].equals("do") || t[0].equals("does")){
- String subj1=subjConvert(t[1]);
- String obj2 =(t.length>3? " " + join(t, 3):"");
- String obj1 = t[2] + obj2;
- if(bd1.containsKey(obj1) || (do1.containsKey(t[1]) && do1.get(t[1]).containsKey(obj1)))
- pr.println("yes, "+subj1+" "+t[2]+(thirdSubj(t[1])? "s":"")+obj2+".");
- else if(bd0.containsKey(obj1)||(do0.containsKey(t[1])&&do0.get(t[1]).containsKey(obj1)))
- pr.println("no, "+subj1+" "+(thirdSubj(t[1])? "doesn't ":"don't ")+t[2]+obj2+".");
- else pr.println("maybe.");
- }else if(t[0].equals("what")){
- TreeSet<pair> objs = new TreeSet(); Map<String, Integer> tmp1 = new HashMap(bd1), tmp0=new HashMap(bd0);
- if(do0.containsKey(t[2])){
- Map<String, Integer> td = do0.get(t[2]);
- for(String e: td.keySet()){
- if(!tmp0.containsKey(e)) tmp0.put(e, td.get(e));
- else tmp0.put(e, Math.min(tmp0.get(e), td.get(e)));
- }
- }
- if(do1.containsKey(t[2])){
- Map<String, Integer> td = do1.get(t[2]);
- for(String e: td.keySet()){
- if(!tmp1.containsKey(e)) tmp1.put(e, td.get(e));
- else tmp1.put(e, Math.min(tmp1.get(e), td.get(e)));
- }
- }
- for(String e: tmp0.keySet())
- objs.add(new pair(tmp0.get(e), (thirdSubj(t[2])? "doesn't ":"don't ") + e));
- for(String e: tmp1.keySet()){
- String [] tt = splitVerbObj(e);
- objs.add(new pair(tmp1.get(e), tt[0]+(thirdSubj(t[2])? "s":"")+tt[1]));
- }
- if(objs.size() == 0) pr.println("I don't know.");
- else if(objs.size()==1) pr.println(subjConvert(t[2]) + " " + objs.first().s + ".");
- else{
- pr.print(subjConvert(t[2]) + " "); int out = 0;
- for(pair e: objs) {
- pr.print(e.s + ", ");
- if(++out == objs.size()-1) break;
- }
- pr.println("and " + objs.last().s+".");
- }
- }else if(t[0].equals("who")){
- String obj1=t[1].substring(0, t[1].length()-1) + (t.length>2? " "+join(t, 2):"");
- String [] tt = splitVerbObj(obj1);
- if(bd1.containsKey(obj1)) pr.println("everybody "+tt[0]+"s"+tt[1]+".");
- else if(bd0.containsKey(obj1)) pr.println("nobody "+tt[0]+"s"+tt[1]+".");
- else{
- List<pair> tmp = new ArrayList(); Map<String, Integer> ob = obj.getOrDefault(obj1, new HashMap());
- for(String e: ob.keySet())
- if(do1.containsKey(e) && do1.get(e).containsKey(obj1)) tmp.add(new pair(ob.get(e), e));
- if(tmp.size()==0) pr.println("I don't know.");
- else {
- Collections.sort(tmp);
- if(tmp.size()==1) {
- pr.println(subjConvert(tmp.get(0).s)+" "+tt[0]+(thirdSubj(tmp.get(0).s)? "s":"")+tt[1]+".");
- }else if(tmp.size()==2){
- pr.println(subjConvert(tmp.get(0).s)+" and " + subjConvert(tmp.get(1).s) + " " + obj1+".");
- }else{
- for(int i=0; i<tmp.size()-2; i++)
- pr.print(subjConvert(tmp.get(i).s)+", ");
- pr.print(subjConvert(tmp.get(tmp.size()-2).s) + " ");
- pr.println("and "+subjConvert(tmp.get(tmp.size()-1).s) + " " + obj1+".");
- }
- }
- }
- }
- }
- pr.println();
- }
- }
- }
- static class pair implements Comparable<pair>{
- public int n; public String s;
- pair(int n0, String s0){ n=n0; s=s0; }
- public int compareTo(pair t){ return Integer.compare(n, t.n); }
- }
- static String next () throws IOException {
- while (st == null || !st.hasMoreTokens())
- st = new StringTokenizer(br.readLine().trim());
- return st.nextToken();
- }
- static long readLong () throws IOException {
- return Long.parseLong(next());
- }
- static int readInt () throws IOException {
- return Integer.parseInt(next());
- }
- static double readDouble () throws IOException {
- return Double.parseDouble(next());
- }
- static String readLine () throws IOException {
- return br.readLine().trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement