Guest User

Untitled

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