public class Main {
private static Node root=
new Node();
public static void main(String[] args) {
Scanner sc=
new Scanner(System.in);
while(sc.hasNextLine()){
String word=sc.nextLine();
if(word.equals(
"")){
break;
}
insert(word);
}
while(sc.hasNextLine()){
String pre=sc.nextLine();
System.out.println(find(pre));
}
sc.close();
}
public static void insert(String
str){
if(
str.isEmpty()||
str==
""){
return;
}
Node cnode=root;
for (
int i =
0; i <
str.length(); i++) {
int index=
str.charAt(i)-
'a';
if(cnode.child[
index]==
null){
Node pnode=
new Node();
cnode.child[
index]=pnode;
}
else{
cnode.child[
index].num++;
}
cnode=cnode.child[
index];
}
cnode.isStr=
true;
}
public static int find(String
str){
if(
str.isEmpty()||
str==
""){
return 0;
}
Node cnode=root;
for (
int i =
0; i <
str.length(); i++) {
int index=
str.charAt(i)-
'a';
if(cnode.child[
index]==
null){
return 0;
}
else{
cnode=cnode.child[
index];
}
}
return cnode.num;
}
}
class Node{
boolean isStr;
int num;
Node[] child;
public Node(){
isStr=
false;
num=
1;
child=
new Node[
26];
}
}
转载请注明原文地址: https://ju.6miu.com/read-1125363.html