【题意概述】

  给出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的更多相关文章

  1. 洛谷p2922[USACO08DEC]秘密消息Secret Message

    题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...

  2. 洛谷 P2922 [USACO08DEC]秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  3. [USACO08DEC] 秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  4. 【题解】P2922 [USACO08DEC]秘密消息Secret Message

    \(\text{Tags}\) 字典树,统计 题意: 给出两组\(\text{0/1}\)串\(\text{A,B}\),求解\(\text{A}\)中某串是\(\text{B}\)中某串的前缀,和\ ...

  5. Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树

    本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...

  6. P2922 [USACO08DEC]秘密消息Secret Message

    传送门 思路: 还是比较水的(不看题解不看书),用 vis 存字典树上的每个点是多少个单词的前缀,bo 来存每个点是多少个单词的结尾(坑点:会有很多相同的单词,不能只有 bool 来存).统计时:① ...

  7. [USACO08DEC] 秘密消息Secret Message (Trie树)

    题目链接 Solution Trie 树水题. 直接将前面所有字符串压入Trie 中. 在查询统计路上所有有单词的地方和最后一个地方以下的单词数即可. Code #include<bits/st ...

  8. Luogu2922 [USACO08DEC]秘密消息Secret Message (Trie树)

    统计以节点\(i\)结尾的数量与经过的数量 #include <iostream> #include <cstdio> #include <cstring> #in ...

  9. 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

随机推荐

  1. bzoj3561

    3561: DZY Loves Math VI Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 240  Solved: 163[Submit][Sta ...

  2. poj Find a multiple【鸽巢原理】

    参考:https://www.cnblogs.com/ACShiryu/archive/2011/08/09/poj2356.html 鸽巢原理??? 其实不用map但是习惯了就打的map 以下C-c ...

  3. ionic2.1.0 --beta3版本新建页面做弹框时遇到的问题

    新建的页面需要在app.module.ts文件中定义.不然制作页面弹出效果是会报错.

  4. php编译安装参数详解

    ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/loc ...

  5. JAXB解析xml 的注解说明

      1.将 xml 文件中的各个节点和属性信息创建对应的Java模型 2.在Java模型中的创建与 xml 文件对应的节点和属性需要用注解来表示@XmlRootElement   将一个Java类映射 ...

  6. Linux学习之路2 Bash的基本操作

    一.SHELL的介绍 shell分为两种:CLI(command Line Interface)和GUI(Graphical User Interface) 操作系统中的shell: GUI:GNOM ...

  7. document.write()、onclick="alert(xxx)、innerHTML、image.src.match("xxx")、id2.style.color="blue";、isNaN(id2)、document.write("糟糕!文档消失了。")、alert(id2.outerHTML)、id2.className="id06";、onclick="return registe"

    <html> <head> <meta charset="utf-8"> <title>javascript</title&g ...

  8. CSS知识点整理(1):CSS语法,层叠次序,选择器,其他重要方面。

    1. css的全称 2. CSS的层叠次序:优先级由低到高 ·浏览器设置 ·外部样式表 或者 内部样式表 —— 就近原则 ·内联样式 3. CSS的3种形式,以及每种形式的语法格式 ——注意样式表的为 ...

  9. 简单的css缩放动画,仿腾讯新闻的分享按钮和美团app底部的图标样式

    最近看到一些好看的hover的图形缩放效果.然后自己就写了下,发现这2种效果都不错.如果伙伴们更好的实现方式可以在下面留言哦~ 还有美团的效果,我就不展示了,喜欢的可以去app应用上看看. 这两种效果 ...

  10. Shell script之How to write

    Write shell script: 1) Editor like vi or mcedi 2) Set execute permission for your script chmod  perm ...