洛谷 2922 BZOJ 1590 [USACO08DEC]秘密消息Secret Message

【题意概述】
给出n个01串组成的字典和m个询问,每次询问某个01串和多少个字典中的串有相同的前缀。(前缀长度是两串中较小的部分)
【题解】
直接上Trie树即可。树上每个节点记录两个信息:这个节点有多少个串经过,这个节点是多少个串的结尾。
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
#define rg register
#define N 500010
using namespace std;
int n,m,a[N][],now,ans,tot;
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
int main(){
memset(a,,sizeof(a));
n=read(); m=read();
for(rg int i=;i<=n;i++){
int x=read(); now=;
while(x--){
int y=read();
if(a[now][y]>) now=a[now][y];
else a[now][y]=++tot,now=a[now][y];
a[now][]++;
}
a[now][]++;
}
while(m--){
int x=read(); bool ok=; ans=now=;
while(x--){
int y=read();
if(a[now][y]&&ok){
now=a[now][y];
if(a[now][]) ans+=a[now][];
}
else ok=;
}
if(ok) ans+=a[now][]-a[now][];
printf("%d\n",ans);
}
return ;
}
洛谷 2922 BZOJ 1590 [USACO08DEC]秘密消息Secret Message的更多相关文章
- 洛谷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 ...
- [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}\)中某串的前缀,和\ ...
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- P2922 [USACO08DEC]秘密消息Secret Message
传送门 思路: 还是比较水的(不看题解不看书),用 vis 存字典树上的每个点是多少个单词的前缀,bo 来存每个点是多少个单词的结尾(坑点:会有很多相同的单词,不能只有 bool 来存).统计时:① ...
- [USACO08DEC] 秘密消息Secret Message (Trie树)
题目链接 Solution Trie 树水题. 直接将前面所有字符串压入Trie 中. 在查询统计路上所有有单词的地方和最后一个地方以下的单词数即可. Code #include<bits/st ...
- Luogu2922 [USACO08DEC]秘密消息Secret Message (Trie树)
统计以节点\(i\)结尾的数量与经过的数量 #include <iostream> #include <cstdio> #include <cstring> #in ...
- 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
随机推荐
- redirect和forward 的区别
1.从地址栏显示来说 forward 是服务器请求资源,服务器直接访问目标地址url,把那个url的响应内容读取过来,然后把这些内容再发给浏览器,浏览器根本不知道服务器发送的内容从哪里来的,所以他的地 ...
- 25.EXTJS 主页面的jsp
1. /** * @author sux * @time 2011-1-11 * @desc main page */ var mainPage = Ext.extend(Ext.Viewport,{ ...
- Spring加载applicationContext.xml实现spring容器管理的单例模式
package com.etc.pojo; import org.springframework.context.ApplicationContext; import org.springframew ...
- 双摆模拟 python(转)
双摆 是 混沌理论 中的一个常见示例,这里通过 python 的作图工具包Matplotlib 来模拟双摆的运动过程: 注意:在 vs2017 中可以正确运行程序,在 cmd 环境下有时报错 &quo ...
- python获取主机名和用户名
import socketimport getpassuser_name = getpass.getuser() # 获取当前用户名hostname = socket.gethostname() # ...
- [转]Mysql之Union用法
转自:http://blog.csdn.net/ganpengjin1/article/details/9090405 MYSQL中的UNION UNION在进行表链接后会筛选掉重复的记录,所以在表链 ...
- js中将html文档写入静态界面当中
1.静态界面当中: <div id="test"></div> 2.在js当中写入 $("#test").append(html文档内容 ...
- Jmeter接口测试---webservices
Jmeter与LR相比来说,测试webservices接口要简单很多. 下面说下具体的操作流程: 1.创建一个线程组 2.创建一个soap/xml请求 3.填写要进行测试webservice的请求内容 ...
- IE 浏览器在地址栏输入中文字符,发送get请求报400错误的问题
因为学校有JavaWeb的课程,所以才接触这方面.最近遇到了个小问题. 先看一段很简单的jsp代码例子 <%@ page language="java" import=&qu ...
- 解决 HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException:
HTTP Status 500 - Unable to show problem report: freemarker.core.InvalidReferenceException: The foll ...