import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
private static class HashedTrie {
private class Trie{
int count;
Map<Character, Trie> children;
Boolean isLeafNode;
public Trie() {
count = 0;
isLeafNode = false;
children = new HashMap<Character, Trie>();
}
public Trie setChild(char ch){
return children.put(ch, new Trie());
}
public Trie getChild(char ch){
return children.get(ch);
}
public int getCount(){
return count;
}
public void setCount(int count){
this.count = count;
}
public Boolean isLeafNode(){
return isLeafNode;
}
public void setLeafNode(Boolean isLeafNode){
this.isLeafNode = isLeafNode;
}
}
Trie trie;
public HashedTrie(){
trie = new Trie();
}
public Boolean InsertString(String s){
Trie head = trie;
head.setCount(head.getCount() + 1);
for(char sChar: s.toCharArray()){
if(head.getChild(sChar) == null){
head.setChild(sChar);
}
head = head.getChild(sChar);
head.setCount(head.getCount() + 1);
}
head.setLeafNode(true);
return true;
}
public int partialSearch(String s){
Trie head = trie;
for(char ch : s.toCharArray()){
head = head.getChild(ch);
if(head == null){
return 0;
}
}
return head.getCount();
}
}
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
List<String> list = new ArrayList();
HashedTrie trie = new HashedTrie();
for (int nItr = 0; nItr < n; nItr++) {
String[] opContact = scanner.nextLine().split(" ");
String op = opContact[0];
String contact = opContact[1];
if(op.contains("add")) {
trie.InsertString(contact);
}
if(op.contains("find")){
int count = trie.partialSearch(contact);
System.out.println(count);
}
}
scanner.close();
}
}
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
private static class HashedTrie {
private class Trie{
int count;
Map<Character, Trie> children;
Boolean isLeafNode;
public Trie() {
count = 0;
isLeafNode = false;
children = new HashMap<Character, Trie>();
}
public Trie setChild(char ch){
return children.put(ch, new Trie());
}
public Trie getChild(char ch){
return children.get(ch);
}
public int getCount(){
return count;
}
public void setCount(int count){
this.count = count;
}
public Boolean isLeafNode(){
return isLeafNode;
}
public void setLeafNode(Boolean isLeafNode){
this.isLeafNode = isLeafNode;
}
}
Trie trie;
public HashedTrie(){
trie = new Trie();
}
public Boolean InsertString(String s){
Trie head = trie;
head.setCount(head.getCount() + 1);
for(char sChar: s.toCharArray()){
if(head.getChild(sChar) == null){
head.setChild(sChar);
}
head = head.getChild(sChar);
head.setCount(head.getCount() + 1);
}
head.setLeafNode(true);
return true;
}
public int partialSearch(String s){
Trie head = trie;
for(char ch : s.toCharArray()){
head = head.getChild(ch);
if(head == null){
return 0;
}
}
return head.getCount();
}
}
public static void main(String[] args) {
int n = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
List<String> list = new ArrayList();
HashedTrie trie = new HashedTrie();
for (int nItr = 0; nItr < n; nItr++) {
String[] opContact = scanner.nextLine().split(" ");
String op = opContact[0];
String contact = opContact[1];
if(op.contains("add")) {
trie.InsertString(contact);
}
if(op.contains("find")){
int count = trie.partialSearch(contact);
System.out.println(count);
}
}
scanner.close();
}
}
No comments:
Post a Comment