acwing 835. Trie字符串统计-java
题目所属分类
idx可以理解为结点
Trie树中有个二维数组 son[N][26]
,表示当前结点的儿子,如果没有的话,可以等于++idx
。Trie树本质上是一颗多叉树,对于字母而言最多有26个子结点。所以这个数组包含了两条信息。比如:son[1][0]=2
表示1结点的一个值为a的子结点为结点2;如果son[1][0] = 0
,则意味着没有值为a子结点。这里的son[N][26]
相当于链表中的ne[N]
。
下标为0 的点 即是空节点也是根结点
void insert(char str[])
{
int p = 0; //从根结点开始遍历
for (int i = 0; i< str.length; i ++ )
{
int u =str[i] - 'a';//将a-z映射成0-25
if (son[p][u] == 0) son[p][u] = ++ idx; //没有该子结点就创建一个
p = son[p][u]; //走到p的子结点
}
cnt[p] ++; // cnt相当于链表中的e[idx]
}
public static int query(char [] arr){
int p = 0 ;
for(int i = 0; i < arr.length ; i++){
int u = arr[i] - 'a';
if(son[p][u] == 0 )return 0;
p =son[p][u];
}
return cnt[p] ;
}
原题链接
代码案例:输入样例:
5
I abc
Q abc
Q ab
I ab
Q ab
输出样例:
1
0
1
题解
import java.util.*;
public class Main{
static int N = 100010;
static int[][] son = new int[N][26];
static int[] cnt= new int[N] ;
static int idx = 0 ;//son是儿子节点 cnt是表示以当前点结尾的儿子有多少个 idx为下标
public static void main(String[] args){
// Scanner scan = new Scanner(System.in);
// int n = scan.nextInt();
// while(n-- > 0){
// String[] arr = scan.nextLine().split(" ");
// String s1 = arr[0];
// String ss = arr[1];
// if(s1.equals("I")){
// insert(ss.toCharArray());
// }else{
// System.out.println(query(ss.toCharArray()));
// }
// }
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
while(n -- > 0){
String s = scan.nextLine();
String[] st = s.split(" ");
String s1 = st[0];
String s2 = st[1];//这总报错 说是超出了范围 但是没有错误 就是通过不了 不知道为什么
if(s1.equals("I")){
insert(s2.toCharArray());
}else{
System.out.println(query(s2.toCharArray()));
}
}
}
public static void insert(char [] arr){
int p = 0 ;
for(int i = 0; i < arr.length ; i++){
int u = arr[i] - 'a';
if(son[p][u] == 0 )++idx;//如果没有创建一个
p =son[p][u];//走向孩子结点
}
cnt[p]++;
}
public static int query(char [] arr){
int p = 0 ;
for(int i = 0; i < arr.length ; i++){
int u = arr[i] - 'a';
if(son[p][u] == 0 )return 0;
p =son[p][u];
}
return cnt[p] ;
}
}
下面的没报错
import java.util.*;
import java.io.*;
public class Main {
private static int[][] son;
private static int[] cnt;
//下标为0的点,既是空节点,又是根节点
private static int idx;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
son = new int[100010][26];
cnt = new int[100010];
int n = Integer.parseInt(in.nextLine());
while (n -- > 0) {
String[] line = in.nextLine().split(" ");
if (line[0].equals("I")) insert(line[1]);
else out.println(query(line[1]));
}
out.flush();
}
public static void insert(String str) {
int p = 0;
for (char c : str.toCharArray()) {
int u = c - 'a';
if (son[p][u] == 0) son[p][u] = ++idx;
p = son[p][u];
}
cnt[p]++;
}
public static int query(String str) {
int p = 0;
for (char c : str.toCharArray()) {
int u = c - 'a';
if (son[p][u] == 0) return 0;
p = son[p][u];
}
return cnt[p];
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)