「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树即可.树上每个节点记录两个信息:这个节点有 ...
随机推荐
- 如何运用spring将dao注入到servlet中?
1.servlet的init方法 public void init(ServletConfig config) throws ServletException { super.init(config) ...
- mongodb的IO测试工具 mongoperf
之前没发现mongoperf这个工具,测试IO的状态用的是iostat来进行观察. mongoperf < myjsonconfigfile echo "{nThreads:2,fi ...
- PowerBuilder -- Tab控件
在tab中关闭窗口 Close(tab_1.getparent()) 调整tab中的控件的tab oder 鼠标右键tabpage_1,选择 Tab Order菜单.
- 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)【转】
启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are alre ...
- android菜鸟学习笔记16----Android项目打包安装过程(Run as Android Application)
右击项目名称,Run as Android Appication之后,Android项目打包安装过程: 1.打包生成.apk文件: 1)把源码中的.java文件编译生成.class文件 2)将所有的. ...
- 淘宝(新浪)API获取IP地址位置信息
package com.parse; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IO ...
- 九度OJ 1030:毕业bg (01背包、DP)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1814 解决:798 题目描述: 每年毕业的季节都会有大量毕业生发起狂欢,好朋友们相约吃散伙饭,网络上称为"bg" ...
- HTML 学习笔记 JQuery(事件)
加载DOM 以浏览器加载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript中,通常使用window.onload方法,在JQuery中通常使 ...
- 我的Android进阶之旅------>Android使用AlarmManager全局定时器实现定时更换壁纸
该DEMO将会通过AlarmManager来周期的调用ChangeService,从而让系统实现定时更换壁纸的功能. 更换壁纸的API为android.app.WallpaperManager,它提供 ...
- 流畅python学习笔记第十八章:使用asyncio包处理并发(二)
前面介绍了asyncio的用法.下面我们来看下如何用协程的方式来实现之前的旋转指针的方法 @asyncio.coroutine def spin(msg): write,flush=sys.stdou ...