题目描述

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. wpf SplitButton

     SplitButton该控件除了本身Button 的功能外,还具有下拉菜单的功能,能够在按键右側加入下拉菜单控件: <SplitButton Content="..." ...

  2. vue proxyTable

    Vue-cli proxyTable 解决开发环境的跨域问题 字数474 阅读1685 评论1 喜欢3 和后端联调时总是会面对恼人的跨域问题,最近基于Vue开发项目时也遇到了这个问题,两边各自想了一堆 ...

  3. [Linux] 概念

    操作系统包括: 内核:管理硬件资源 库:没有执行入口的程序,用于提升软件开发效率 应用程序:有执行入口的程序 常见库文件: windows系统:dll(dynamic link library)动态链 ...

  4. JavaScript -- JavaScript DOM 编程艺术(第2版)

    /* 渐进增强 平稳退化 网页 结构层(structural layer): HTML 表示层(presentation layer): CSS <link rel="styleshe ...

  5. JavaWeb学习总结第二篇--第一个JavaWeb程序

    JavaWeb学习总结第二篇—第一个JavaWeb程序 最近我在学院工作室学习并加入到研究生的项目中,在学长学姐的带领下,进入项目实践中,为该项目实现一个框架(用已有框架进行改写).于是我在这里记录下 ...

  6. 【BZOJ】1003 Cards

    [解析]Burnside引理+背包dp+乘法逆元 [Analysis] 这道题卡了好久,就是没想懂置换跟着色是不一样的. 依据burnside引理.在一个置换群作用下不等价类的个数为每一个置换作用下不 ...

  7. 微信小程序设置控件权重

    项目中最常用的两种布局方式,水平布局和垂直布局,在微信小程序中实现起来也比较简单.       1.横向水平布局:         实现水平布局,需要四个view容器组件,其中一个是父容器.如下: & ...

  8. Large-scale Incremental Processing Using Distributed Transactions and Notifications

    Large-scale Incremental Processing Using Distributed Transactions and Notifications

  9. 我的Java开发学习之旅------>Java语言中方法的参数传递机制

    实参:如果声明方法时包含来了形参声明,则调用方法时必须给这些形参指定参数值,调用方法时传给形参的参数值也被称为实参. Java的实参值是如何传入方法?这是由Java方法的参数传递机制来控制的,Java ...

  10. 我的Android进阶之旅------>Android使用正则表达式匹配扫描指定目录下的所有媒体文件(音乐、图像、视频文件)

    今天使用正则表达式匹配指定目录下的所有媒体文件,下面将这份代码简化了,可以收藏下来,当作工具类. package match; import java.io.File; import java.uti ...