「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述
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.
输入输出样例
说明
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自动机的更多相关文章
- 洛谷p2922[USACO08DEC]秘密消息Secret Message
题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
- [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
题目描述 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 ...
- 洛谷 2922 BZOJ 1590 [USACO08DEC]秘密消息Secret Message
[题意概述] 给出n个01串组成的字典和m个询问,每次询问某个01串和多少个字典中的串有相同的前缀.(前缀长度是两串中较小的部分) [题解] 直接上Trie树即可.树上每个节点记录两个信息:这个节点有 ...
随机推荐
- Android中经常使用的bitmap处理方法
收集了非常多bitmap相关的处理方法,差点儿所有应用在项目中,所以特记录下! package com.tmacsky.utils; import java.io.ByteArrayOutputStr ...
- Linux模块机制浅析_转
Linux模块机制浅析 转自:http://www.cnblogs.com/fanzhidongyzby/p/3730131.htmlLinux允许用户通过插入模块,实现干预内核的目的.一直以来,对l ...
- 如何利用JQuery获取iframe内联框架对象?
parent.$("#iframeID").get(0).contentWindow; 父.$("选择器").get(0).contentWindow; get ...
- android Bluetooth-蓝牙
bluetooth 一.开启蓝牙 1.获取BluetoothAdapter BluetoothAdapter.getDefaultAdapter() 2.判断手机设备是否 有蓝牙模块 3.开启蓝牙设备 ...
- 【BZOJ4619/3709】[Wf2016]Swap Space/[PA2014]Bohater 贪心
[BZOJ4619][Wf2016]Swap Space Description 你有许多电脑,它们的硬盘用不同的文件系统储存数据.你想要通过格式化来统一文件系统.格式化硬盘可能使它的容量发生变化.为 ...
- async & await (转载)
async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是async的味道了.但是这也给我们 编程埋下了一些 ...
- 高德地图API开发二三事(一)如何判断点是否在折线上及引申思考
最近使用高德地图 JavaScript API 开发地图应用,提炼了不少心得,故写点博文,做个系列总结一下,希望能帮助到LBS开发同胞们. 项目客户端使用高德地图 JavaScript API,主要业 ...
- 【题解】P3796【模板】AC自动机(加强版)
[题解]P3796 [模板]AC自动机(加强版) 记录当前\(cnt\)是第几个"星".记录第几个串是对应着第几个星. 这里补充一点对于\(AC\)自动机的理解.可能一直有个问题我 ...
- Java for LeetCode 098 Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- spring-boot3代码
App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...