「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树即可.树上每个节点记录两个信息:这个节点有 ...
随机推荐
- List集合的遍历方法
估计你永远都不会忘记这三个方法了...... public static void main(String[] args) { //超级for循环遍历方法 List<String> lis ...
- .NET C# 【小技巧】控制台程序,运行是否弹出窗口选择!
选中控制台程序项目,右键→属性→应用程序栏→输出类型: 1.Windows 应用程序(不弹出提示框)! 2.控制台应用程序(弹出提示框)! 3.类库(类库生成dll,是不能直接运行的,类库供应用程序调 ...
- POJ2407_Relatives【欧拉phi函数】【基本】
Relatives Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11422 Accepted: 5571 Descriptio ...
- iOS - 集成SDK问题
1.大部分社交平台接口不支持https协议. 问题描述:在iOS9下,系统默认会拦截对http协议接口的访问,因此无法获取http协议接口的数据.对ShareSDK来说,具体表现可能是,无法授权.分享 ...
- yum 安装 mysql5.5 mysql 5.6 mysql5.7
一. yum 安装mysql5.6 1. 安装仓库 要使用yum 安装mysql,需要使用mysql的yum 仓库,先从官网下载适合你的系统仓库 http://dev.mysql.com/down ...
- Python中属性
属性定义的两种方式: 1.num1=property(GetNum,SetNum) class Pro(): def __init__(self): self._num= def GetNum(s ...
- 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:248 解决:194 题目描述: A number of students sit in a circle facing their teac ...
- git拉取远程分支到本地分支或者创建本地新分支
git fetch origin branchname:branchname 可以把远程某各分支拉去到本地的branchname下,如果没有branchname,则会在本地新建branchname g ...
- 高德地图API开发二三事(一)如何判断点是否在折线上及引申思考
最近使用高德地图 JavaScript API 开发地图应用,提炼了不少心得,故写点博文,做个系列总结一下,希望能帮助到LBS开发同胞们. 项目客户端使用高德地图 JavaScript API,主要业 ...
- 使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器
参考: 1,使用nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器笔记(一)http://blog.csdn.net/xdwyyan/article/details/4319 ...