洛谷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 ...
随机推荐
- nginx 初探 之反向代理
首先要解释的是什么叫做反向代理? 平时我们浏览网页可以输入网址直接访问, 但如果访问国外的网站, 可能就没那么简单('中国特色'), 这时候我们需要配置一个代理服务器, 然后通过此服务器中转来访 ...
- 将本地项目和远程git仓库相连接
有时候写代码,是存在本地的,远程仓库没有对应的代码库,这个时候就需要把本地的代码项目与远程git库连接并推送. 1. 将项目文件添加到仓库中 本地的项目文档: 添加项目文件 git add . 2. ...
- vee-validate
http://vee-validate.logaretm.com/ 表单校验,配合VUE使用
- hdu 3118 Arbiter
http://acm.hdu.edu.cn/showproblem.php?pid=3118 题意:删除最少的边使图没有奇环 二分图的定义:如果顶点能分为两个互不相交的子集,则图为二分图 二分 ...
- CF864 E DP 输出路径
n个物品有Deadline,拿物品需要花费时间,问取得最大价值的方案. 本质是个01背包,先按时间排序,然后把花费的时间作为背包就行了. 主要就是找方案,倒过来找发生转移的就行了. 太菜了真的不会打C ...
- ① 设计模式的艺术-07.适配器(Adapter)模式
什么是适配器模式? 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 模式中的角色 目标接口(Target):客户所期待的接口 ...
- ④ 设计模式的艺术-04.抽象工厂(Abstract Factory)模式
抽象工厂模式 用来生产不同产品族的全部产品.(对于增加新的产品,无能为力:支持增加产品族) 抽象工厂模式是工厂方法模式的升级版本,在有多个业务品种.业务分类时,通过抽象工厂模式产生需要的对象是一种非常 ...
- Tomcat处理一个http请求的过程
假设来自客户的请求为: http://localhost:8080/wsota/wsota_index.jsp 1) 请求被发送到本机端口8080,被在那里侦听的Coyote HTTP/1.1 Con ...
- Intellij IDEA 快捷键整理(转)
Ctrl+Shift + Enter,语句完成 “!”,否定完成,输入表达式时按 “!”键 Ctrl+E,最近的文件 Ctrl+Shift+E,最近更改的文件 Shift+Click,可以关闭文件 C ...
- 引用类型 ( 对象定义 )——Object 类型
本文地址:http://www.cnblogs.com/veinyin/p/7607100.html 创建实例 new 操作符后跟构造函数 var people = new Object(); pe ...