int n = nextInt(); while (n-- != 0) { String s = nextString(); char[] sb = s.toCharArray(); insert(sb); } int m = nextInt(); while (m-- != 0) { String s = nextString(); char[] sb = s.toCharArray(); System.out.println(query(sb)); }
closeAll(); } //============================================================ // 插入一个字符串 publicstaticvoidinsert(char[] str){ int p = 0; for (int i = 0; i < str.length; ++i) { int v = str[i] - 'a'; if (son[p][v] == 0) son[p][v] = ++idx; p = son[p][v]; } cnt[p]++; }
// 查询字符串出现的次数 publicstaticintquery(char[] str){ int p = 0; for (int i = 0; i < str.length; ++i) { int v = str[i] - 'a'; if (son[p][v] == 0) return0; p = son[p][v]; } return cnt[p]; } //============================================================