Guest User

Untitled

a guest
Feb 6th, 2019
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.    
  6.     void solve() {
  7.         int t = in.nextInt();
  8.         HashMap<String, HashMap<String, Integer>> adj = new HashMap<>();
  9.         HashSet<String> all = new HashSet<>();
  10.         long pairs = 0;
  11.         for (int tt = 0; tt < t; tt++) {
  12.             String s = in.next();
  13.             String prev = null;
  14.             for (int i = 0; i < s.length() - 2; i++) {
  15.                 String w = s.substring(i, i + 3);
  16.                 all.add(w);
  17.                 if (!adj.containsKey(w)) {
  18.                     adj.put(w, new HashMap<>());
  19.                 }
  20.                 if (prev != null) {
  21.                     int old = 0;
  22.                     if (adj.get(prev).containsKey(w)) {
  23.                         old = adj.get(prev).get(w);
  24.                     } else {
  25.                         pairs++;
  26.                     }
  27.                     int incremented = old + 1;
  28.                     adj.get(prev).put(w, incremented);
  29.                 }
  30.                 prev = w;
  31.             }
  32.         }
  33.         out.println(all.size());
  34.         out.println(pairs);
  35.         for (String a : adj.keySet()) {
  36.             for (String b : adj.get(a).keySet()) {
  37.                 out.println(a + " " + b + " " + adj.get(a).get(b));
  38.             }
  39.         }
  40.     }
  41.  
  42.    
  43.  
  44.     Scanner in;
  45.     PrintWriter out;
  46.  
  47.     void run() {
  48.         try {
  49.             in = new Scanner(new File("A.in"));
  50.             out = new PrintWriter(new File("A.out"));
  51.  
  52.             solve();
  53.  
  54.             out.close();
  55.         } catch (FileNotFoundException e) {
  56.             e.printStackTrace();
  57.         }
  58.     }
  59.  
  60.     void runIO() {
  61.  
  62.         in = new Scanner(System.in);
  63.         out = new PrintWriter(System.out);
  64.  
  65.         solve();
  66.  
  67.         out.close();
  68.     }
  69.  
  70.     class Scanner {
  71.         BufferedReader br;
  72.         StringTokenizer st;
  73.  
  74.         public Scanner(File f) {
  75.             try {
  76.                 br = new BufferedReader(new FileReader(f));
  77.             } catch (FileNotFoundException e) {
  78.                 e.printStackTrace();
  79.             }
  80.         }
  81.  
  82.         public Scanner(InputStream f) {
  83.             br = new BufferedReader(new InputStreamReader(f));
  84.         }
  85.  
  86.         String next() {
  87.             while (st == null || !st.hasMoreTokens()) {
  88.                 String s = null;
  89.                 try {
  90.                     s = br.readLine();
  91.                 } catch (IOException e) {
  92.                     e.printStackTrace();
  93.                 }
  94.                 if (s == null)
  95.                     return null;
  96.                 st = new StringTokenizer(s);
  97.             }
  98.             return st.nextToken();
  99.         }
  100.  
  101.         private boolean isSpaceChar(int c) {
  102.             return !(c >= 33 && c <= 126);
  103.         }
  104.  
  105.         private int skip() {
  106.             int b;
  107.             while ((b = read()) != -1 && isSpaceChar(b)) ;
  108.             return b;
  109.         }
  110.  
  111.         private int read() {
  112.             int res = -1;
  113.             try {
  114.                 res = br.read();
  115.             } catch (IOException e) {
  116.                 e.printStackTrace();
  117.             }
  118.             return res;
  119.         }
  120.  
  121.         char[] nextCharArray(int size) {
  122.             char[] buf = new char[size];
  123.             int b = skip(), p = 0;
  124.             while (p < size && !(isSpaceChar(b))) {
  125.                 buf[p++] = (char) b;
  126.                 b = read();
  127.             }
  128.             return size == p ? buf : Arrays.copyOf(buf, p);
  129.         }
  130.  
  131.         char[][] nextCharMap(int n, int m) {
  132.             char[][] map = new char[n][];
  133.             for (int i = 0; i < n; i++) {
  134.                 map[i] = nextCharArray(m);
  135.             }
  136.             return map;
  137.         }
  138.  
  139.         boolean hasMoreTokens() {
  140.             while (st == null || !st.hasMoreTokens()) {
  141.                 String s = null;
  142.                 try {
  143.                     s = br.readLine();
  144.                 } catch (IOException e) {
  145.                     e.printStackTrace();
  146.                 }
  147.                 if (s == null)
  148.                     return false;
  149.                 st = new StringTokenizer(s);
  150.             }
  151.             return true;
  152.         }
  153.  
  154.         int nextInt() {
  155.             return Integer.parseInt(next());
  156.         }
  157.  
  158.         long nextLong() {
  159.             return Long.parseLong(next());
  160.         }
  161.  
  162.         double nextDouble() {
  163.             return Double.parseDouble(next());
  164.         }
  165.     }
  166.  
  167.     public static void main(String[] args) {
  168.         new Main().runIO();
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment