题目描述

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:

  1. 4 5
  2. 3 0 1 0
  3. 1 1
  4. 3 1 0 0
  5. 3 1 1 0
  6. 1 0
  7. 1 1
  8. 2 0 1
  9. 5 0 1 0 0 1
  10. 2 1 1
输出样例#1:

  1. 1
  2. 3
  3. 1
  4. 1
  5. 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

trie树

屠龙宝刀点击就送

  1. #include <cstring>
  2. #include <cstdio>
  3. #define N 200000
  4.  
  5. int a[N],m,n,num[N],cnt[N],end[N],trie[N][],fail[N],siz=;
  6. inline void ins(int L)
  7. {
  8. int p=;
  9. for(int i=;i<=L;++i)
  10. {
  11. if(!trie[p][a[i]]) trie[p][a[i]]=++siz;
  12. p=trie[p][a[i]];
  13. cnt[p]++;
  14. }
  15. end[p]++;
  16. }
  17. int query(int L)
  18. {
  19. int p=,ans=,f=;
  20. for(int i=;i<=L;++i)
  21. {
  22. p=trie[p][a[i]];
  23. if(!p) {f=;break;}
  24. ans+=end[p];
  25. }
  26. if(!f) return ans+cnt[p]-end[p];
  27. else return ans;
  28. }
  29. int main()
  30. {
  31. scanf("%d%d",&m,&n);
  32. for(int l,i=;i<=m;++i)
  33. {
  34. scanf("%d",&l);
  35. for(int j=;j<=l;++j) scanf("%d",&a[j]);
  36. ins(l);
  37. }
  38. for(int l;n--;)
  39. {
  40. scanf("%d",&l);
  41. for(int i=;i<=l;++i) scanf("%d",&a[i]);
  42. printf("%d\n",query(l));
  43. }
  44. return ;
  45. }

洛谷 P2922 [USACO08DEC]秘密消息Secret Message的更多相关文章

  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. 【题解】P2922 [USACO08DEC]秘密消息Secret Message

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

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

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

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

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

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

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

  7. [USACO08DEC] 秘密消息Secret Message

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

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

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

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

    统计以节点\(i\)结尾的数量与经过的数量 #include <iostream> #include <cstdio> #include <cstring> #in ...

随机推荐

  1. 爬虫库之BeautifulSoup学习(一)

    Beautiful Soup的简介 简单来说,Beautiful Soup是python的一个库,最主要的功能是从网页抓取数据. 官方解释如下: Beautiful Soup提供一些简单的.pytho ...

  2. 【eclipse插件开发实战】Eclipse插件开发7——插件发布jar包

    Eclipse插件开发7--插件发布jar包 最省事的方式就是直接导出jar包,然后放到eclipse的plugins目录下,重启eclipse即可. step1: 对需要打包的插件工程右击→导出(E ...

  3. Flutter实战视频-移动电商-36.FlutterToast插件使用

    36.FlutterToast插件使用 https://github.com/PonnamKarthik/FlutterToast fluttertoast: ^ category_page.dart ...

  4. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 25. 过滤器

    在MVC的请求管道 并不是  asp.net core的请求管道.所以说Filter是专用于MVC的 贯穿特性,横穿关注点.比如授权.日志 这里的Authorize其实就是一个Filter,主要用来授 ...

  5. git 回退到服务器版本操作

    git fetch git reset orgin master --hard git pull

  6. WinMain和main

    WinMain的原型: int WINAPI WinMain(HINSTANCE hinstance,//程序本身的实例句柄                                  HINS ...

  7. bzoj 2055: 80人环游世界【有上下界有源汇最小费用最大流】

    连有上下界的边(ss,i,(0,m),0),(i',t,(0,m),0),表示从任意点开始和结束 连(i,j,(0,m),d[i][j]),表示可以买票飞过去 连(i,i',(v[i],v[i]),0 ...

  8. A - Wireless Network

    #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> ...

  9. AVL树(自平衡二叉查找树)

    了解AVL树之前要先了解二叉查找树(BST),BST查找元素的时间复杂度平均是O(logN),最坏的情况是O(N),所有的元素都接在左子树(或者右子树)就相当于一串链表了.而AVL树会对子树过高的情况 ...

  10. laravel之null替换空字符串中间件

    在laravel写接口的时候免不了数据库中保存null,可用诸如设置ORM的访问器或以下方法处理 $goods->name?$goods->name:''; 其实可以利用路由中间件,在需要 ...