Advertisement
chmilar

Untitled

Feb 23rd, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.text.*;
  4. import java.math.*;
  5. import java.util.regex.*;
  6.  
  7. public class Solution {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner in = new Scanner(System.in);
  11.         int n = in.nextInt();
  12.         TrieNode trie = new TrieNode();
  13.         StringBuilder sb = new StringBuilder();
  14.         for(int a0 = 0; a0 < n; a0++){
  15.             String op = in.next();
  16.             String contact = in.next();
  17.             if ("add".equals(op)) {
  18.                 trie.insert(contact);
  19.             } else if ("find".equals(op)) {
  20.                 sb.append(trie.matchCount(contact));
  21.                 sb.append("\n");
  22.             }
  23.         }
  24.         System.out.println(sb.toString());
  25.     }
  26. }
  27.  
  28. class TrieNode {
  29.     HashMap<Character, TrieNode> children;
  30.     boolean isCompleteWord;
  31.     boolean addedChildSinceCount;
  32.     int fullChildWords;
  33.    
  34.    
  35.     public TrieNode() {
  36.         children = new HashMap<>();
  37.         isCompleteWord = false;
  38.         addedChildSinceCount = true;
  39.     }
  40.    
  41.     public int matchCount(String needle) {
  42.         if (needle == null || needle.isEmpty()) return 0;
  43.         TrieNode dfsRoot = prefixSubset(needle);
  44.         if (dfsRoot == null)
  45.             return 0;
  46.         return dfsRoot.dfsCount(0);
  47.     }
  48.    
  49.     private int dfsCount(int count) {
  50.         if (addedChildSinceCount) {
  51.             for (TrieNode t : children.values()) {
  52.                 fullChildWords += t.dfsCount(count);
  53.             }
  54.             addedChildSinceCount = false;
  55.         }
  56.         if (isCompleteWord)
  57.             ++count;
  58.         return count + fullChildWords;
  59.     }
  60.    
  61.    
  62.    
  63.     public TrieNode prefixSubset(String needle) {
  64.         TrieNode child = children.get(needle.charAt(0));
  65.         if (child == null)
  66.             return null;
  67.         if (needle.length() == 1)
  68.             return child;
  69.         return child.prefixSubset(needle.substring(1));
  70.     }
  71.  
  72.    
  73.     public void insert(String word) {
  74.         addedChildSinceCount = true;
  75.         fullChildWords = 0;
  76.         char c = word.charAt(0);
  77.         TrieNode child = children.get(c);
  78.         if (child == null) {
  79.             child = new TrieNode();
  80.             children.put(c, child);
  81.         }
  82.         if (word.length() == 1) {
  83.             child.isCompleteWord = true;
  84.         } else {
  85.             child.insert(word.substring(1));
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement