题目描述

Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret binary messages to each other.

Ever the clever counterspy, Farmer John has intercepted the first b_i (1 <= b_i <= 10,000) bits of each of M (1 <= M <= 50,000) of these secret binary messages.

He has compiled a list of N (1 <= N <= 50,000) partial codewords that he thinks the cows are using. Sadly, he only knows the first c_j (1 <= c_j <= 10,000) bits of codeword j.

For each codeword j, he wants to know how many of the intercepted messages match that codeword (i.e., for codeword j, how many times does a message and the codeword have the same initial bits). Your job is to compute this number.

The total number of bits in the input (i.e., the sum of the b_i and the c_j) will not exceed 500,000.

Memory Limit: 32MB

POINTS: 270

贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.

信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位.

对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者.

在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.

输入输出格式

输入格式:

* Line 1: Two integers: M and N

* Lines 2..M+1: Line i+1 describes intercepted code i with an integer b_i followed by b_i space-separated 0's and 1's

* Lines M+2..M+N+1: Line M+j+1 describes codeword j with an integer c_j followed by c_j space-separated 0's and 1's

输出格式:

* Lines 1..M: Line j: The number of messages that the jth codeword could match.

输入输出样例

输入样例#1:
复制

4 5
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
输出样例#1: 复制

1
3
1
1
2

说明

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.

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

题解

这种很多个串要匹配前缀什么的,就上AC自动姬就好了。

在建Trie树的同时记录经过每个点的串个数($pas$),和在每个点结尾的串个数($tag$)。

匹配的时候,在匹配路上加上途径的$tag$,在匹配结尾加上结尾点的$pas$。

然后如果在匹配中途就无路可走了的话,就到此为止,也不要加结尾点的$pas$。

注意一下边界就好啦。

 /*
qwerta
P2922 [USACO08DEC]秘密消息Secret Message
Accepted
100
代码 C++,1.02KB
提交时间 2018-10-16 08:14:21
耗时/内存
709ms, 3964KB
*/
#include<iostream>
#include<cstdio>
using namespace std;
struct emm{
int nxt[],tag,pas;
}a[];
int main()
{
//freopen("a.in","r",stdin);
int n,m;
scanf("%d%d",&n,&m);
int cnt=;
for(int i=;i<=n;++i)
{
int c;
scanf("%d",&c);
int k=;
//建Trie树
for(int j=;j<=c;++j)
{
int x;
scanf("%d",&x);
if(!a[k].nxt[x])
a[k].nxt[x]=++cnt;
a[k].pas++;
k=a[k].nxt[x];
}
//cout<<k<<endl;
a[k].tag++;
}
for(int i=;i<=m;++i)
{
int c;
scanf("%d",&c);
int k=,ans=;
int flag=;
for(int j=;j<=c;++j)
{
int x;
scanf("%d",&x);
//cout<<k<<" "<<x<<" "<<a[k].nxt[x]<<endl;
if(!a[k].nxt[x])flag++;//如果无路可走就记个flag
if(!flag)//如果没有过flag就走
k=a[k].nxt[x],
ans+=a[k].tag;//加上途径的tag
}
if(!flag)//如果一路走道了底
cout<<ans+a[k].pas<<endl;//就加上结尾的pas
else
cout<<ans<<endl;//否则只输出途径的tag
}
return ;
}

「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机的更多相关文章

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

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

  2. 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]

    洛谷传送门,BZOJ传送门 秘密消息Secret Message Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共有M(1≤M≤5 ...

  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

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

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

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

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

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

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

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

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

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

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

    [题意概述] 给出n个01串组成的字典和m个询问,每次询问某个01串和多少个字典中的串有相同的前缀.(前缀长度是两串中较小的部分) [题解] 直接上Trie树即可.树上每个节点记录两个信息:这个节点有 ...

随机推荐

  1. Linux系统下授权MySQL账户访问指定数据库和数据库操作

    Linux系统下授权MySQL账户访问指定数据库 需求: 1.在MySQL中创建数据库mydata 2.新建MySQL账户admin密码123456 3.赋予账户admin对数据库mydata具有完全 ...

  2. Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容

    © 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

  3. 基于JQuery实现表单元素值的回写

    form.jsp: <%@ page language="java" import="java.util.*" pageEncoding="GB ...

  4. scikit-learn(project中用的相对较多的模型介绍):2.3. Clustering(可用于特征的无监督降维)

    參考:http://scikit-learn.org/stable/modules/clustering.html 在实际项目中,我们真的非常少用到那些简单的模型,比方LR.kNN.NB等.尽管经典, ...

  5. 个人笔记-CSS

    http://localhost:1081/sdfsdfs/config-browser/actionNames.action 超出容器文字隐藏 .hiddenoverflowtext { width ...

  6. eclipse--windowBuilder

    https://www.eclipse.org/windowbuilder/ https://www.eclipse.org/windowbuilder/download.php Documentat ...

  7. J2EE——开发环境搭建

    WEB环境搭建 1.J2EE开发环境搭建(1)——安装JDK.Tomcat.Eclipse 2.JAVA运行环境和J2EE运行环境的搭建 3.jsp开发所需要的eclipse插件(lomboz.tom ...

  8. 16 redis之sentinel运维监控

    一:sentinel运维监控 Sentinel不断与master通信,获取master的slave信息. 监听master与slave的状态 如果某slave失效,直接通知master去除该slave ...

  9. 自动关闭AfxMessageBox对话框―模拟"回车" VC

    有的时候,在程序里面调用太多的AfxMessageBox(非调试用),弹出的对话框要手动关闭,时间一长就感觉很繁琐.于是上网找了一些资料,发现有一个很简单的实现AfxMessageBox对话框自动关闭 ...

  10. mongodb启动不了:提示错误信息为 child process failed, exited with error number 100

    [启动mongo 副本集错误提示]: [原因分析说明]: 查询很多资料得知由于上次使用了暴力关闭系统或者DB,导致数据文件锁住. [解决办法]: 1.  在 mongo.conf 文件添加一下属性值  ...