题目描述
blablabla
样例
blablabla
算法1
Java 代码
import java.io.*;
import java.util.*;
class Main{
static class TrieNode{
public int count;
public TrieNode[] children;
public TrieNode() {
children = new TrieNode[26];
count = 0;
}
}
private static TrieNode root;
public static void insert(String s){
int n = s.length();
TrieNode p = root;
for(int i = 0; i < n; i++){
int index = (int)(s.charAt(i) - 'a');
if(p.children[index] == null){
p.children[index] = new TrieNode();
}
p = p.children[index] ;
}
p.count++;
}
public static int search(String s){
int n = s.length();
TrieNode p = root;
for(int i = 0; i < n; i++){
int index = (int)(s.charAt(i) - 'a');
if(p.children[index] == null){
return 0;
}
p = p.children[index];
}
return p.count;
}
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws Exception{
int n = Integer.valueOf(read.readLine());
String[] s;
root = new TrieNode();
for(int i = 0; i < n; i++){
s = read.readLine().split(" ");
if("I".equals(s[0])){
insert(s[1]);
}else if("Q".equals(s[0])){
log.write(search(s[1]) + "\n");
}
}
log.flush();
}
}