[USACO08DEC] 秘密消息Secret Message (Trie树)
题目链接
Solution
Trie 树水题。
直接将前面所有字符串压入Trie 中.
在查询统计路上所有有单词的地方和最后一个地方以下的单词数即可.
Code
#include<bits/stdc++.h>
using namespace std;
const int maxn=500005;
int ch[maxn][10];
int num[maxn],pd[maxn];
int n,m,tot;
int c[maxn];
int read()
{
char ch=getchar(); int f=1,w=0;
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){w=w*10+ch-'0';ch=getchar();}
return f*w;
}
void insert()
{
int u=0,p,len=read();
for(int i=0;i<len;i++)
{
p=read();
if(!ch[u][p])
ch[u][p]=++tot;
u=ch[u][p];
num[u]++;
}
num[u]--;
pd[u]++;
return;
}
void query()
{
int u=0,ans=0,p,len=read(),flag=0;
for(int i=0;i<len;i++)
{
p=read();
ans+=pd[ch[u][p]];
if(ch[u][p])
u=ch[u][p];
else {if(!flag)
cout<<ans<<endl;flag=1;}
}
ans+=(num[u]);
if(!flag)
cout<<ans<<endl;
return;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
insert();
for(int i=1;i<=m;i++)
query();
}
[USACO08DEC] 秘密消息Secret Message (Trie树)的更多相关文章
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- 洛谷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 ...
- 洛谷 2922 BZOJ 1590 [USACO08DEC]秘密消息Secret Message
[题意概述] 给出n个01串组成的字典和m个询问,每次询问某个01串和多少个字典中的串有相同的前缀.(前缀长度是两串中较小的部分) [题解] 直接上Trie树即可.树上每个节点记录两个信息:这个节点有 ...
- Luogu2922 [USACO08DEC]秘密消息Secret Message (Trie树)
统计以节点\(i\)结尾的数量与经过的数量 #include <iostream> #include <cstdio> #include <cstring> #in ...
- [USACO08DEC] 秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- 【题解】P2922 [USACO08DEC]秘密消息Secret Message
\(\text{Tags}\) 字典树,统计 题意: 给出两组\(\text{0/1}\)串\(\text{A,B}\),求解\(\text{A}\)中某串是\(\text{B}\)中某串的前缀,和\ ...
- P2922 [USACO08DEC]秘密消息Secret Message
传送门 思路: 还是比较水的(不看题解不看书),用 vis 存字典树上的每个点是多少个单词的前缀,bo 来存每个点是多少个单词的结尾(坑点:会有很多相同的单词,不能只有 bool 来存).统计时:① ...
随机推荐
- fork新建进程
#include <sys/types.h>#include<sys/wait.h>#include<unistd.h>#include<stdio.h> ...
- freespace
这是一个总结freespace的博客,还不错,但算法目前看来比较老:https://blog.csdn.net/u010213183/article/details/70786516
- lua调用java过程
在cocos2dx框架中,有继承好的luaj文件来方便我们去使用lua调用java底层代码,注意:luaj只能使用在安卓平台下,如果在平台下使用,会出错, 所以使用前需要加平台判断,方法 如下: lo ...
- c++ 拷贝资源方法
#include "stdio.h" #include "stdlib.h" #include <sys/types.h> #include < ...
- ARC中__weak;__strong;__unsafe_unretained;修饰词
测试代码: // Human.h代码 @interface Human : NSObject @property (nonatomic, weak) Cat *pinkCat; @property ( ...
- TypeError: Cannot read property 'tap' of undefined
E:\vue-project\vue-element-admin-master>npm run build:prod vue-element-admin@3.8.1 build:prod E:\ ...
- k8s 基于NFS部署storageclass pv自动供给
在k8s中部署有状态应用时,通常需要做数据持久化存储. 后端存储的方式有以下几种: 1.基于宿主机本地的存储方式: (重启pod时,若pod被调度到其他节点上,尽管原来节点上的数据不会丢失,但是其他节 ...
- COMP9021--6.6
1. 在print结尾处添加end='' print默认在字符串结尾处添加换行符,添加end=''后表示这个语句并没有结束,结尾不换行 2. 为了减少重复代码以及便于修改,我们可以编写函数 1) 函数 ...
- [译]The Python Tutorial#6. Modules
[译]The Python Tutorial#Modules 6. Modules 如果你从Python解释器中退出然后重新进入,之前定义的名字(函数和变量)都丢失了.因此,如果你想写长一点的程序,使 ...
- 线段树:CDOJ1591-An easy problem A (RMQ算法和最简单的线段树模板)
An easy problem A Time Limit: 1000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...