141 字典树的实现
Table of Contents

ac

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

class Trie{

    Node head = new Node(0);

    void insert(String word){
        if(word == null){
            return;
        }
        char[] words = word.toCharArray();
        Node t = head;
        for (int i = 0; i < words.length; i++) {
            int index = words[i] - 'a';
            if (t.next[index] == null) {
                t.next[index] = new Node(0);
            }
            t = t.next[index];
            t.count++;
        }
        t.wordCnt ++;
    }

    void delete(String word){
        if(!search(word)) {
            return;
        }
        char[] words = word.toCharArray();
        Node t = head;
        for (int i = 0; i < words.length; i++) {
            int index = words[i] - 'a';
            Node tPre = t;
            t = t.next[index];
            t.count--;
            if(t.count == 0){ // 直接删除
                tPre.next[index] = null;
            }
        }
        if(t != null) {
            t.wordCnt--;
        }
    }

    boolean search(String word){
        if(word == null){
            return false;
        }
        char[] words = word.toCharArray();
        Node t = head;
        for(int i=0;i<words.length;i++){
            int index = words[i] - 'a';
            if(t.next[index] == null){
                return false;
            }
            t = t.next[index];
        }
        if(t.wordCnt >= 1) { // 遍历完了,要是单词
            return true;
        }
        return false;
    }

    // 如果有单词出现了两次就按两次来算
    int prefixNumber(String pre){
        if(pre == null){
            return 0;
        }
        char[] word = pre.toCharArray();
        int len = word.length;
        Node t = head;
        for(int i=0;i<len;i ++)
        {
            int tmp = word[i]-'a';
            if(t.next[tmp] == null) {
                return 0;
            }
            t = t.next[tmp];
        }
        return t.count;
    }

    class Node{
        int count;  // 有多少个单词使用了该节点
        int wordCnt; // 以该节点截止的单词数量
        Node[] next;

        public Node(int count) {
            this.count = count;
            next = new Node[27];
            wordCnt = 0;
            for(int i=0;i<27;i++){
                next[i] = null;
            }
        }
    }
}

public class Main {
    public static Scanner sc = new Scanner(System.in);

    public static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    public static long mod = (long)Math.pow(2,29);

    public static void main(String[] args) throws Exception {
        String nStr = reader.readLine();
        int n = Integer.valueOf(nStr);

        Trie trie = new Trie();
        for(int i=0;i<n;i++){
            String line = reader.readLine();
            String[] opWord = line.split(" ");
            int op = Integer.valueOf(opWord[0]);
            String word = opWord[1];
            if(op == 1){
                trie.insert(word);
            }
            if(op == 2){
                trie.delete(word);
            }
            if(op == 3){
                if(trie.search(word)){
                    System.out.println("YES");
                }else {
                    System.out.println("NO");
                }
            }
            if(op == 4){
                int preCnt = trie.prefixNumber(word);
                System.out.println(preCnt);
            }
        }
    }

}
/*
7
1 qwer
1 qwe
3 qwer
4 q
2 qwer
3 qwer
4 q

YES
2
NO
1
*/