洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
秘密消息Secret Message
Description
Input
Output
Sample Input
3 0 1 0
1 1
3 1 0 0
3 1 1 0
1 0
1 1
2 0 1
5 0 1 0 0 1
2 1 1
INPUT DETAILS:
Four messages; five codewords.
The intercepted messages start with 010, 1, 100, and 110.
The possible codewords start with 0, 1, 01, 01001, and 11.
Sample Output
3
1
1
2
HINT
0 matches only 010: 1 match 1 matches 1, 100, and 110: 3 matches 01 matches only 010: 1 match 01001 matches 010: 1 match 11 matches 1 and 110: 2 matches
分析:
并不难的一道$Trie$树。
题目中的前缀长度为信息串与密码串中较短的一个,那么我们可以这么做,在$Trie$树的每一个节点记录一个$value$,表示该节点由多少个单词使用过,在每一个信息串的末尾记录一个$id$,表示该单词共出现过几次。然后比较的时候统计经过的所有完整的单词数,然后如果扫完了整个密码串,我们需要判断一下当前扫到的位置是否是末端点,如果不是还需要把包含该节点但不在该节点处结尾的单词数统计一下。注意细节即可。(说的比较绕,看不懂就直接看代码吧)
Code:
//It is made by HolseLee on 12th Aug 2018
//luogu.org P2922
#include<bits/stdc++.h>
using namespace std; const int N=5e5+;
int n,m,tot,s[N];
struct Node{
int nx[],val,id;
}t[N];
struct Trie{
void ins()
{
int root=;
for(int i=;i<=s[];++i){
if(!t[root].nx[s[i]]){
t[root].nx[s[i]]=++tot;
t[t[root].nx[s[i]]].val=;
}
root=t[root].nx[s[i]];
t[root].val++;
if(i==s[])t[root].id++;
}
} int quary()
{
int root=,ret=;bool flag=false;
for(int i=;i<=s[];++i){
root=t[root].nx[s[i]];
if(!root){
flag=true;break;
}
ret+=t[root].id;
}
if(!flag)ret+=(t[root].val-t[root].id);
return ret;
}
}T; inline int read()
{
char ch=getchar();int num=;bool flag=false;
while(ch<''||ch>''){if(ch=='-')flag=true;ch=getchar();}
while(ch>=''&&ch<=''){num=num*+ch-'';ch=getchar();}
return flag?-num:num;
} int main()
{
n=read();m=read();
t[].val=;
for(int i=;i<=n;++i){
s[]=read();
for(int j=;j<=s[];++j)s[j]=read();
T.ins();
}
for(int i=;i<=m;++i){
s[]=read();
for(int j=;j<=s[];++j)s[j]=read();
printf("%d\n",T.quary());
}
return ;
}
洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]的更多相关文章
- 洛谷p2922[USACO08DEC]秘密消息Secret Message
题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...
- 洛谷 P2922 [USACO08DEC]秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- [USACO08DEC] 秘密消息Secret Message (Trie树)
题目链接 Solution Trie 树水题. 直接将前面所有字符串压入Trie 中. 在查询统计路上所有有单词的地方和最后一个地方以下的单词数即可. Code #include<bits/st ...
- 【题解】P2922 [USACO08DEC]秘密消息Secret Message
\(\text{Tags}\) 字典树,统计 题意: 给出两组\(\text{0/1}\)串\(\text{A,B}\),求解\(\text{A}\)中某串是\(\text{B}\)中某串的前缀,和\ ...
- P2922 [USACO08DEC]秘密消息Secret Message
传送门 思路: 还是比较水的(不看题解不看书),用 vis 存字典树上的每个点是多少个单词的前缀,bo 来存每个点是多少个单词的结尾(坑点:会有很多相同的单词,不能只有 bool 来存).统计时:① ...
- 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- 洛谷 P3102 [USACO14FEB]秘密代码Secret Code 解题报告
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
- 洛谷P3102 [USACO14FEB]秘密代码Secret Code
P3102 [USACO14FEB]秘密代码Secret Code 题目描述 Farmer John has secret message that he wants to hide from his ...
随机推荐
- libcurl在mingw下编译
通过命令提示符进入 curl-7.27.0 文件夹输入 mingw32-make mingw32 进行生成(这里我只需要普通的功能,于是没有加附加的选项)编译完成后,在 lib 文件夹中会有我们需要的 ...
- LeetCode-Insertion Sort List[AC源码]
package com.lw.leet5; /** * @ClassName:Solution * @Description: * Insertion Sort List * Sort a linke ...
- 写一个简易web服务器、ASP.NET核心知识(4)
前言 昨天尝试了,基于对http协议的探究,我们用控制台写了一个简单的浏览器.尽管浏览器很low,但是对于http协议有个更好的理解. 说了上面这一段,诸位猜到我要干嘛了吗?(其实不用猜哈,标题里都有 ...
- NYOJ 202 红黑树 (二叉树)
题目链接 描述 什么是红黑树呢?顾名思义,跟枣树类似,红黑树是一种叶子是黑色果子是红色的树... 当然,这个是我说的... <算法导论>上可不是这么说的: 如果一个二叉查找树满足下面的红黑 ...
- Redis数据类型之散列(hash)
1. 什么是散列 散列类似于一个字典,是一个<K, V>对的集合,不过这个key和value都只能是字符串类型的,不能嵌套,可以看做Java中的Map<String, String& ...
- Mac 下安装 ruby 环境解决 brew 安装 yarn 问题
在brew安装yarn提示 ruby的版本过低.在网上搜了一下发现 1. mac下自带的ruby 在 system 目录下 2. 其实可以用brew安装一个ruby brew install ruby ...
- JavaScript实现水平进度条拖拽效果
<html> <head> <meta charset="UTF-8"> <title>Document</title> ...
- 64_p9
python2-termcolor-1.1.0-11.fc26.noarch.rpm 12-Feb-2017 14:05 13610 python2-terminado-0.6-2.fc26.noar ...
- elasticsearch集群介绍及优化【转】
elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展)或者购买更多的服务器(横向扩展),Elasticsearch能从更强大的硬件中获得更好的性能,但是纵 ...
- ECharts图表tooltip显示时超出canvas图层解决方法
我们在做ECharts图表的时候可能会遇到tooltip显示时超出了canvas图层范围,并且被其它z-index较高的div容器遮盖,这是悬浮展示信息就看不全,我们根据官网文档的配置项查询发现con ...