Guest User

Untitled

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