1. 程式人生 > 其它 >Luogu P3311 [SDOI2014] 數數 題解

Luogu P3311 [SDOI2014] 數數 題解

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int m = in.nextInt();
            Trie trie = new Trie();
            while (m-- > 0) {
                int op = in.nextInt();
                String word = in.next();
                if (op == 1) {
                    trie.insert(word);
                } else if (op == 2) {
                    trie.delete(word);
                } else if (op == 3) {
                    System.out.println(trie.search(word) ? "YES" : "NO");
                } else {
                    System.out.println(trie.prefixNumber(word));
                }
            }
        }
    }
}

class Trie {

    private static final int DEFAULT_CHILD_NUM = 26;

    private static final TrieNode root = new TrieNode();

    static class TrieNode {
        int freq;
        int end;
        Map<Character, TrieNode> children;

        public TrieNode() {
            this.freq = 0;
            this.end = 0;
            this.children = new HashMap<>(DEFAULT_CHILD_NUM);
        }
    }

    public void insert(String word) {
        if (word == null || word.length() == 0) {
            return;
        }
        TrieNode current = root;
        for (int i = 0; i < word.length(); ++i) {
            char value = word.charAt(i);
            TrieNode child = current.children.get(value);
            if (child == null) {
                child = new TrieNode();
                current.children.put(value, child);
            }
            child.freq++;
            current = child;
        }
        current.end++;
    }

    public void delete(String word) {
        if (word == null || word.length() == 0) {
            return;
        }
        TrieNode current = root;
        for (int i = 0; i < word.length(); ++i) {
            char value = word.charAt(i);
            TrieNode child = current.children.get(value);
            if (child == null) {
                return;
            }
            child.freq--;
            current = child;
        }
        current.end--;
    }


    public boolean search(String word) {
        if (word == null || word.length() == 0) {
            return false;
        }
        TrieNode current = root;
        for (int i = 0; i < word.length(); ++i) {
            char value = word.charAt(i);
            TrieNode child = current.children.get(value);
            if (child == null) {
                return false;
            }
            current = child;
        }
        return current.end > 0;
    }

    public int prefixNumber(String word) {
        if (word == null || word.length() == 0) {
            return 0;
        }
        TrieNode current = root;
        for (int i = 0; i < word.length(); ++i) {
            char value = word.charAt(i);
            TrieNode child = current.children.get(value);
            if (child == null) {
                return 0;
            }
            current = child;
        }
        return current.freq;
    }
}
心之所向,素履以往 生如逆旅,一葦以航