1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import java.util.Scanner;
public class Main { private static int N=100010; private static int son[][]=new int[N][26]; private static int cnt[]=new int[N]; private static char str[]=new char[N]; private static int idx;
public static void main(String[] args){ Scanner scanner=new Scanner(System.in); int n=scanner.nextInt(); while (n-->0){ String op=scanner.next(); String str=scanner.next(); if("I".equals(op)){ insert(str.toCharArray()); }else if("Q".equals(op)){ System.out.println(query(str.toCharArray())); } } }
private static void insert(char[] str) { int p=0; for(int i=0;i<str.length ;i++){ int u=str[i]-'a'; if(son[p][u]==0) son[p][u]=++idx; p=son[p][u]; } cnt[p]++; }
private static int query(char[] str) { int p=0; for(int i=0;i<str.length;i++){ int u=str[i]-'a'; if(son[p][u]==0) return 0; p=son[p][u]; } return cnt[p]; } }
|