Pagini recente » Cod sursa (job #1073408) | Cod sursa (job #1470688) | Cod sursa (job #3279675) | Cod sursa (job #1987953) | Cod sursa (job #1415488)
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;
private int count;
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 boolean decrementCount() {
if ( count > 0 ) {
count--;
return true;
}
return false;
}
public boolean hasChildWithChar(char childChar) {
return children[childChar-'a'] != null;
}
public Node getChildWithChar(char childChar) {
return children[childChar-'a'];
}
public boolean addChildNode(Node node) {
if ( !hasChildWithChar(node.getCharacter()) ) {
children[node.getCharacter()-'a'] = node;
childCount++;
return true;
}
return false;
}
public boolean removeChildNode(char character) {
if ( hasChildWithChar(character) ) {
children[character-'a'] = null;
childCount--;
return true;
}
return false;
}
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) ) {
if ( pointer.getCount() > 0 ) { // optimization
stack.clear();
}
stack.push(pointer); // saving the tree path in case we need to remove some or all the nodes
pointer = pointer.getChildWithChar(nextChar);
} else {
return false; // string to delete not found
}
}
if ( pointer.getCount() > 0 ) {
if ( pointer.getCount() == 1 && !pointer.hasChildren() ) {
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;
}
}