835. 字符串统计(Trie树模板题)

维护一个字符串集合,支持两种操作:

  1. “I x”向集合中插入一个字符串x;
  2. “Q x”询问一个字符串在集合中出现了多少次。

共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母。

输入格式

第一行包含整数N,表示操作数。

接下来N行,每行包含一个操作指令,指令为”I x”或”Q x”中的一种。

输出格式

对于每个询问指令”Q x”,都要输出一个整数作为结果,表示x在集合中出现的次数。

每个结果占一行。

数据范围

1≤N≤2∗1041≤N≤2∗104

输入样例:

5
I abc
Q abc
Q ab
I ab
Q ab

输出样例:

1
0
1

import java.util.Scanner;

public class Main{
        static final int max=100005;
        static int son[][]=new int[max][26];//son数组存储树节点;
        static int cnt[]=new int[max];      //cnt[i]存储以下标i为结点的字符串的数量;
        static int idx;                     //idx表示当前用到了那个下标,下标是0的点既是根节点又是空节点
        static void insert(String s){
                int p=0;
                for(int i=0;i<s.length();i++){
                        int u=s.charAt(i)-‘a‘;
                        if(son[p][u]==0) son[p][u]=++idx;
                        p=son[p][u];
                }
                cnt[p]++;
        }
        static int query(String s){
                int p=0;
                for(int i=0;i<s.length();i++){
                        int u=s.charAt(i)-‘a‘;
                        if(son[p][u]==0) return 0;
                        p=son[p][u];
                }
                return cnt[p];
        }
         public static void main(String[] args) {
                 Scanner scan=new Scanner(System.in);
                 int n=scan.nextInt();
                 while(n-->0){
                         String s=scan.next();
                         if(s.equals("I")){
                                 String str=scan.next();
                                 insert(str);
                         }
                         else{
                                 String str=scan.next();
                                 System.out.println(query(str));
                         }
                 }
        }
}

原文地址:https://www.cnblogs.com/qdu-lkc/p/12234088.html

时间: 2024-07-29 19:51:18

835. 字符串统计(Trie树模板题)的相关文章

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

hdu 4828 Xor Sum (trie 树模板题,经典应用)

hdu 4825 题目链接 题意:给定n个数,然后给出m个询问,每组询问一个数x,问n中的数y使得x和y的异或和最大. 思路:字典树..把每个数转化成二进制,注意补全前导0,使得所有数都有相同的位数. 如果想要异或和最大,那么每一位尽可能都是1. 所以做法是,先构建字典树,然后每次find的时候,尽可能按照和当前寻找的数的位相反的位的方向走(如果有的话) 比如当前位是1,那我就往0的方向走. 需要注意的是,多组数据,每次要重新初始化一遍. 做法是 在struct 中重新 root = new N

HiHo1014 : Trie树(Trie树模板题)

描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小Ho提出了那个经典的问题:“小Ho,你能不能对于每一个我给出的字符串,都在这个词典里面找到以这个字符串开头的所有单词呢?” 身经百战的小Ho答道:“怎么会不能呢!你每给我一个字符串,我就依次遍历词典里的所有单词,检查你给我的字符串是不是这个单词的前缀不就是了?” 小Hi笑道:“你啊,还是太年轻了!~假设这本词典里有10万个单

hihocoder_1014: Trie树(Trie树模板题)

题目链接 #include<bits/stdc++.h> using namespace std; const int L=12; struct T { int num; T* next[26]; T() { num=0; int i; for(int i=0;i<26;i++) next[i]=NULL; } }t; void insert(char str[]) { T* p=&t; for(int i=0;str[i];i++) { int a=str[i]-'a'; if

HDU 4085 斯坦纳树模板题

Dig The Wells Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 971    Accepted Submission(s): 416 Problem Description You may all know the famous story "Three monks". Recently they find som

字典树模板题 POJ 2503

1 #include <cstdio> 2 #include <cstring> 3 4 char en[11],fr[11]; 5 int st; 6 struct Tire{ 7 int next[26]; 8 char eng[11]; 9 }node[200005]; 10 void insert(char *s,int cur) 11 { 12 if(*s){ 13 if(!node[cur].next[*s-'a']) 14 node[cur].next[*s-'a']

[POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; int n,q,tot,a[110000]; in

Trie树 模板

typedef struct node { int count; struct node *next[MAX]; }Trie; Trie *Newnode()//建立结点&初始化a { int i; Trie *T; T = (Trie *)malloc(sizeof(Trie)); T->count = 0; for(i=0;i<MAX;i++) T->next[i] = NULL; return T; } void creatTrie(Trie *root, char *st