地址  https://www.acwing.com/problem/content/description/837/

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

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

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

输入格式

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

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

输出格式

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

每个结果占一行。

数据范围

1≤N≤2∗104

输入样例:

I abc
Q abc
Q ab
I ab
Q ab
输出样例:

trie 树的模板题

解法1 使用trie树解决

#include <iostream>

using namespace std;

const int N = ;

int son[N][], cnt[N], idx;
char str[N]; void insert(char *str)
{
int p = ;
for (int i = ; str[i]; i ++ )
{
int u = str[i] - 'a';
if (!son[p][u]) son[p][u] = ++ idx;
p = son[p][u];
}
cnt[p] ++ ;
} int query(char *str)
{
int p = ;
for (int i = ; str[i]; i ++ )
{
int u = str[i] - 'a';
if (!son[p][u]) return ;
p = son[p][u];
}
return cnt[p];
} int main()
{
int n;
scanf("%d", &n);
while (n -- )
{
char op[];
scanf("%s%s", op, str);
if (*op == 'I') insert(str);
else printf("%d\n", query(str));
} return ;
}

解法2  使用哈希解决

#include <iostream>
#include <map>
#include <string> using namespace std; map<string,int> re; const int N = ; int son[N][], cnt[N], idx;
char str[N]; void insert(char str[]){
re[str]++;
} int query(char str[]){
return re[str];
} int main()
{
int n;
scanf("%d", &n);
while (n -- )
{
char op[];
scanf("%s%s", op, str);
if (*op == 'I') insert(str);
else printf("%d\n", query(str));
} return ;
}

输入样例:

5
I abc
Q abc
Q ab
I ab
Q ab

输出样例:

1
0
1
难度:简单
时/空限制:1s / 64MB
总通过数:632
总尝试数:828
来源:模板题

acwing 835. Trie字符串统计的更多相关文章

  1. Acwing.835. Trie字符串统计(模板)

    维护一个字符串集合,支持两种操作: “I x”向集合中插入一个字符串x: “Q x”询问一个字符串在集合中出现了多少次. 共有N个操作,输入的字符串总长度不超过 105105,字符串仅包含小写英文字母 ...

  2. 算法基础——Trie字符串统计

    原题链接 题目: 维护一个字符串集合,支持两种操作: "I x"向集合中插入一个字符串x: "Q x"询问一个字符串在集合中出现了多少次. 共有N个操作,输入的 ...

  3. Trie树模板1字符串统计

    Trie树模板1字符串统计 我们首先来了解一下字典树,首先看一下一张字典树的图片 字典树就是一个可以高效存储.查找字符串的树,比如上面这个字典树就是存储abc,acb,bac的字典树. 1.插入操作( ...

  4. HDOJ2017字符串统计

    字符串统计 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. HDOJ2017_字符串统计

    这是一道水题 HDOJ2017_字符串统计 #include<iostream> #include<string> #include<stdio.h> #inclu ...

  6. golang 字符串统计

    golang内建只认utf8 如果传递的字符串里含有汉字什么的,最好使用 utf8.RuneCountInString() 统计 字符串统计几种方法: - 使用 bytes.Count() 统计- 使 ...

  7. 给出一个string字符串,统计里面出现的字符个数

    给出一个string字符串,统计里面出现的字符个数 解决方案: 使用algorithm里面的count函数,使用方法是count(begin,end,'c'),其中begin指的是起始地址,end指的 ...

  8. hdu2017 字符串统计【C++】

    字符串统计 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. 【Java例题】8.2 手工编写字符串统计的可视化程序

      2. 手工编写字符串统计的可视化程序. 一个Frame窗体容器,布局为null,两个TextField组件,一个Button组件. Button组件上添加ActionEvent事件监听器Actio ...

随机推荐

  1. Checkedlistbox只能单选不能多选

    private void Checkedlistbox_ItemCheck(object sender, ItemCheckEventArgs e) { ; i < chkCountry.Ite ...

  2. Springboot vue.js html 跨域 前后分离 Activiti6 工作流 集成代码生成器 shiro 权限

    官网:www.fhadmin.org 特别注意: Springboot 工作流  前后分离 + 跨域 版本 (权限控制到菜单和按钮) 后台框架:springboot2.1.2+ activiti6.0 ...

  3. Linux(Centos7)下Mysql的安装

    1.1 查看mysql的安装路径: [root@bogon ~]# whereis mysql mysql: /usr/bin/mysql /usr/lib/mysql /usr/share/mysq ...

  4. C#&.Net干货分享- 构造SMSWebChineseApi对接第三方短信平台

    你们想要的短信对接,要舍得点钱哦.... /// <summary>    /// http://sms.webchinese.cn/api.shtml    /// </summa ...

  5. 开启docker

    systemctl daemon-reload systemctl restart docker.service

  6. IDE开发小技巧-快速引包/替换关键词

    快速引包 Ctrl+Shift+O 快速搜索/查找替换   Ctrl+F

  7. [译]Vulkan教程(18)命令buffers

    [译]Vulkan教程(18)命令buffers Command buffers 命令buffer Commands in Vulkan, like drawing operations and me ...

  8. HashMap、HashTable 和 ConcurrentHashMap 线程安全问题

    一.HashMap HashMap 是线程不安全的. JDK 1.7 HashMap 采用数组 + 链表的数据结构,多线程背景下,在数组扩容的时候,存在 Entry 链死循环和数据丢失问题. JDK ...

  9. PAT 1006 Sign In and Sign Out 查找元素

    At the beginning of every day, the first person who signs in the computer room will unlock the door, ...

  10. C#以对象为成员的例子

    using System; using System.Collections.Generic; using System.Text; namespace test { class Program { ...