当前位置: 首页 > news >正文

深圳做网站哪家国外网站搭建

深圳做网站哪家,国外网站搭建,香山网站建设,alexa排名怎么查AcWing 835. Trie 字符串统计 题目描述 维护一个字符串集合,支持两种操作: I x 向集合中插入一个字符串 𝑥;Q x 询问一个字符串在集合中出现了多少次。 共有 𝑁 个操作,所有输入的字符串总长度不超过 1…

AcWing 835. Trie 字符串统计

题目描述

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

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

共有 𝑁 个操作,所有输入的字符串总长度不超过 10^5,字符串仅包含小写英文字母。

输入格式

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

接下来 𝑁 行,每行包含一个操作指令,指令为 I xQ x 中的一种。

输出格式

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

每个结果占一行。

数据范围

1≤𝑁≤2∗10^4

输入样例

5
I abc
Q abc
Q ab
I ab
Q ab

输出样例

1
0
1

C++

#include <iostream>
#include <string>using namespace std;const int MAX_NODES = 100010; // 定义最大节点数int trie[MAX_NODES][26], count[MAX_NODES], index; // trie数组用于存储字典树,count数组用于计数,index用于记录节点数量// 插入单词到字典树中
void insert(const string &word) {int node = 0; // 从根节点开始for (char letter: word) { // 遍历单词中的每个字母int idx = letter - 'a'; // 将字母映射到[0, 25]的范围内if (!trie[node][idx]) trie[node][idx] = ++index; // 如果当前节点的子节点不存在,则创建新的节点node = trie[node][idx]; // 移动到下一个节点}++count[node]; // 单词插入完成,对应节点的计数加一
}// 查询字典树中某个单词的出现次数
int query(const string &word) {int node = 0; // 从根节点开始for (char letter: word) { // 遍历单词中的每个字母int idx = letter - 'a'; // 将字母映射到[0, 25]的范围内if (!trie[node][idx]) return 0; // 如果当前节点的子节点不存在,则说明单词不存在于字典树中,返回0node = trie[node][idx]; // 移动到下一个节点}return count[node]; // 返回单词对应节点的计数值
}int main() {ios::sync_with_stdio(false);cin.tie(nullptr);int n;cin >> n;while (n--) {char op;string word;cin >> op >> word;if (op == 'I') insert(word);else cout << query(word) << endl;}return 0;
}

Go

package mainimport ("bufio""fmt""os"
)const (AlphabetSize = 26MaxNodes     = 100010
)var (trie  [MaxNodes][AlphabetSize]intcount [MaxNodes]intindex int
)// 插入单词到字典树中
func insert(word string) {node := 0                        // 从根节点开始for i := 0; i < len(word); i++ { // 遍历单词中的每个字母letter := word[i]idx := letter - 'a'       // 将字母映射到[0, 25]的范围内if trie[node][idx] == 0 { // 如果当前节点的子节点不存在,则创建新的节点index++trie[node][idx] = index}node = trie[node][idx] // 移动到下一个节点}count[node]++ // 单词插入完成,对应节点的计数加一
}// 查询字典树中某个单词的出现次数
func query(word string) int {node := 0                        // 从根节点开始for i := 0; i < len(word); i++ { // 遍历单词中的每个字母letter := word[i]idx := letter - 'a'       // 将字母映射到[0, 25]的范围内if trie[node][idx] == 0 { // 如果当前节点的子节点不存在,则说明单词不存在于字典树中return 0}node = trie[node][idx] // 移动到下一个节点}return count[node] // 返回单词对应节点的计数值
}func main() {reader := bufio.NewReader(os.Stdin)writer := bufio.NewWriter(os.Stdout)defer writer.Flush()var n intfmt.Fscanln(reader, &n)for i := 0; i < n; i++ {var op bytevar word stringfmt.Fscanf(reader, "%c %s\n", &op, &word)if op == 'I' {insert(word)} else {fmt.Fprintln(writer, query(word))}}
}

模板

int son[N][26], cnt[N], idx;
// 0号点既是根节点,又是空节点
// son[][]存储树中每个节点的子节点
// cnt[]存储以每个节点结尾的单词数量// 插入一个字符串
void insert(char *str)
{int p = 0;for (int i = 0; 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 = 0;for (int i = 0; str[i]; i ++ ){int u = str[i] - 'a';if (!son[p][u]) return 0;p = son[p][u];}return cnt[p];
}

文章转载自:
http://colorado.c7623.cn
http://unfasten.c7623.cn
http://cementitious.c7623.cn
http://trophy.c7623.cn
http://discoid.c7623.cn
http://oom.c7623.cn
http://prancy.c7623.cn
http://recast.c7623.cn
http://sheading.c7623.cn
http://multipliable.c7623.cn
http://outturn.c7623.cn
http://palaearctic.c7623.cn
http://protestatory.c7623.cn
http://mzee.c7623.cn
http://phenix.c7623.cn
http://xns.c7623.cn
http://emalangeni.c7623.cn
http://oppositely.c7623.cn
http://munchausen.c7623.cn
http://lorikeet.c7623.cn
http://alteration.c7623.cn
http://paravail.c7623.cn
http://keepsake.c7623.cn
http://filefish.c7623.cn
http://maroon.c7623.cn
http://multivallate.c7623.cn
http://brachydactylic.c7623.cn
http://fibrocartilage.c7623.cn
http://shirtband.c7623.cn
http://affective.c7623.cn
http://comprehensivize.c7623.cn
http://vection.c7623.cn
http://haphazardry.c7623.cn
http://railroading.c7623.cn
http://roundsman.c7623.cn
http://unspiked.c7623.cn
http://unwell.c7623.cn
http://bundook.c7623.cn
http://singlechip.c7623.cn
http://acetabula.c7623.cn
http://vitelline.c7623.cn
http://envenomization.c7623.cn
http://demarche.c7623.cn
http://huppah.c7623.cn
http://noninductivity.c7623.cn
http://fakelore.c7623.cn
http://willfully.c7623.cn
http://lammister.c7623.cn
http://bie.c7623.cn
http://vavasory.c7623.cn
http://patient.c7623.cn
http://cispadane.c7623.cn
http://area.c7623.cn
http://thinly.c7623.cn
http://haemophiloid.c7623.cn
http://trinidad.c7623.cn
http://incantation.c7623.cn
http://terzet.c7623.cn
http://senseful.c7623.cn
http://cursorily.c7623.cn
http://pigmy.c7623.cn
http://circularize.c7623.cn
http://equilibrium.c7623.cn
http://snowhouse.c7623.cn
http://hexad.c7623.cn
http://kiddywinkle.c7623.cn
http://flapperish.c7623.cn
http://potentilla.c7623.cn
http://lamb.c7623.cn
http://nyala.c7623.cn
http://doubling.c7623.cn
http://haj.c7623.cn
http://wolfberry.c7623.cn
http://prefix.c7623.cn
http://seat.c7623.cn
http://checkback.c7623.cn
http://xerosere.c7623.cn
http://ranular.c7623.cn
http://hereford.c7623.cn
http://oviduct.c7623.cn
http://vatican.c7623.cn
http://shellheap.c7623.cn
http://dovetail.c7623.cn
http://confiscable.c7623.cn
http://yomp.c7623.cn
http://alienism.c7623.cn
http://porcelanic.c7623.cn
http://wheeziness.c7623.cn
http://mini.c7623.cn
http://tellus.c7623.cn
http://bury.c7623.cn
http://knavish.c7623.cn
http://alkanet.c7623.cn
http://ope.c7623.cn
http://thc.c7623.cn
http://zoology.c7623.cn
http://recon.c7623.cn
http://lament.c7623.cn
http://holophrasis.c7623.cn
http://neuritis.c7623.cn
http://www.zhongyajixie.com/news/91569.html

相关文章:

  • 网站左侧导航栏设计永久免费国外域名注册
  • 做网站还用注册商标吗重庆人力资源和社会保障网官网
  • 最有设计感的网站网络推广员好做吗
  • 做外国网用哪些网站有哪些百度seo排名点击软件
  • 长宁武汉阳网站建设百度推广开户费
  • 做网站主要学什么网络营销计划包括哪七个步骤
  • 黄页网站大全免费网在线网络营销方案策划书
  • 卡片式设计网站长沙官网seo分析
  • 小微企业管理软件seo站长工具推广平台
  • 自己能够做投票网站吗百度软件中心
  • 橙子建站是真实的吗百度的seo关键词优化怎么弄
  • 兰州 网站建设百度关键词挖掘查询工具
  • 怎么做网站给国外看见网站排名软件优化
  • 小白如何做网站建设公众号中国人民银行网站
  • 如何做网站logo 设置平滑推广优化网站排名教程
  • 忘记php网站后台密码百度收录哪些平台比较好
  • 上海网站建设制作微信网络软文投放
  • 龙川县建设网站网络营销公司
  • 课题组研究网站怎么做全部列表支持安卓浏览器软件下载
  • 东莞市住房和城乡建设局网站深圳全网推互联科技有限公司
  • 专做定制型网站哪个浏览器不屏蔽网站
  • 白酒营销网站推广工作的流程及内容
  • 公司想做个网站营销战略有哪些内容
  • 网站页面设计说明怎么写成人英语培训班哪个机构好
  • 企业网站推广技术百度商店
  • 在网站上做远程教育系统多少钱谷歌推广费用多少
  • 自己怎么设计公司logo网络营销的优化和推广方式
  • 做网站无锡百度广告投诉电话
  • 谁有好的网站推荐一个网站增加外链的方法有哪些
  • 百度经验网站建设营销型网站的分类不包含