Cod sursa(job #1409034)

Utilizator ibicecIT Zilla ibicec Data 30 martie 2015 13:03:04
Problema Trie Scor 40
Compilator java Status done
Runda Arhiva educationala Marime 4.49 kb
import java.io.*;
import java.util.*;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        Trie trie = new Trie();
        Scanner scanner = new Scanner(new FileReader("trie.in"));
        PrintWriter writer = new PrintWriter("trie.out");
        while ( scanner.hasNext() ) {
            String operationId = scanner.next();
            int operation = Integer.parseInt(operationId);
            String word = scanner.next();
            switch (operation) {
                case 0:
                    trie.add(word);
                    break;
                case 1:
                    trie.delete(word);
                    break;
                case 2:
                    writer.println(trie.find(word));
                    break;
                case 3:
                    writer.println(trie.longPrefix(word));
                    break;
            }
        }
        scanner.close();
        writer.close();
    }
}

class Node {
    private char character = '\0';
    private int count = 0;
    private Node[] children = new Node[26];

    private int childCount = 0;

    Node() {
    }

    Node(char character) {
        this.character = character;
    }

    public char getCharacter() {
        return character;
    }

    public int getCount() {
        return count;
    }

    public void incrementCount() {
        count++;
    }

    public void decrementCount() {
        count--;
    }

    public boolean hasChildWithChar(char childChar) {
        return children[childChar-'a'] != null;
    }

    public Node getChildWithChar(char childChar) {
        return children[childChar-'a'];
    }

    public void addChildNode(Node node) {
        children[node.getCharacter()-'a'] = node;
        childCount++;
    }

    public void removeChildNode(char character) {
        children[character-'a'] = null;
        childCount--;
    }

    public boolean hasChildren() {
        return childCount > 0;
    }
}

class Trie {
    private Node root = new Node();

    public void add(String word) {
        Node pointer = root;
        for (int i=0; i<word.length(); i++) {
            char nextChar = word.charAt(i);
            if ( pointer.hasChildWithChar(nextChar) ) {
                pointer = pointer.getChildWithChar(nextChar);
            } else {
                Node childNode = new Node(nextChar);
                pointer.addChildNode(childNode);
                pointer = childNode;
            }
        }
        pointer.incrementCount();
    }

    public int find(String word) {
        Node pointer = root;
        for (int i=0; i<word.length(); i++) {
            char nextChar = word.charAt(i);
            if ( !pointer.hasChildWithChar(nextChar) ) {
                return 0;
            } else {
                pointer = pointer.getChildWithChar(nextChar);
            }
        }
        return pointer.getCount();
    }

    public int longPrefix(String word) {
        int prefixLen = 0;
        Node pointer = root;
        for (int i=0; i<word.length(); i++) {
            char nextChar = word.charAt(i);
            if ( pointer.hasChildWithChar(nextChar) ) {
                pointer = pointer.getChildWithChar(nextChar);
                prefixLen++;
            } else {
                break;
            }
        }
        return prefixLen;
    }

    public boolean delete(String word) {
        Node pointer = root;
        Stack<Node> stack = new Stack<Node>();
        for (int i=0; i<word.length(); i++) {
            char nextChar = word.charAt(i);
            if ( pointer.hasChildWithChar(nextChar) ) {
                stack.push(pointer);
                pointer = pointer.getChildWithChar(nextChar);
            } else {
                return false;
            }
        }

        if ( pointer.getCount() > 0 ) {
            if ( pointer.getCount() == 1 ) {
                while ( !stack.empty() ) {
                    char delChar = pointer.getCharacter();
                    pointer = stack.pop();
                    pointer.removeChildNode(delChar);
                    if ( pointer.getCount() > 0 || pointer.hasChildren() ) {
                        break;
                    }
                }
            } else {
                pointer.decrementCount();
            }
        } else {
            return false; // count is supposed to be greater than 0
        }

        return true;
    }
}