2794: [Usaco2008 Dec]Secret Message 秘密信息

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 7  Solved: 3
[Submit][Status][Web Board]

Description

贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.
信息是二进制的,共有M(1≤M≤50000)条.反间谍能力很强的约翰已经部分拦截了这些信息,知道了第i条二进制信息的前bi(l《bi≤10000)位.他同时知道,奶牛使用N(1≤N≤50000)条密码.但是,他仅仅了解第J条密码的前cj(1≤cj≤10000)位.
对于每条密码J,他想知道有多少截得的信息能够和它匹配.也就是说,有多少信息和这条密码有着相同的前缀.当然,这个前缀长度必须等于密码和那条信息长度的较小者.
在输入文件中,位的总数(即∑Bi+∑Ci)不会超过500000.

Input

第1行输入N和M,之后N行描述秘密信息,之后M行描述密码.每行先输入一个整数表示信息或密码的长度,之后输入这个信息或密码.所有数字之间都用空格隔开.

Output

共M行,输出每条密码的匹配信息数.

Sample Input

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
INPUT DETAILS:
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.

Sample Output

1
3
1
1
2
//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

HINT

 

Source

题解:

   额,trie树乱搞题,用一个size记录有几个儿子,sum记录是尾节点的次数,遍历trie树,如果不是尾节点就加上sum,是尾节点就加上size。

#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 500005
using namespace std;
int n,m,k,tot,ans;
int size[maxn],sum[maxn];
int son[maxn][];
int a[maxn];
char ch;
void read(int &x){
for (ch=getchar();!isdigit(ch);ch=getchar());
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
}
void insert(int k)
{
int p=;
for (int i=; i<=k; p=son[p][a[i]],i++)
{
if (!son[p][a[i]]) son[p][a[i]]=++tot;
size[p]++;
}
sum[p]++;
size[p]++;
}
void workout(int k)
{
int p=; ans=;
for (int i=; i<=k; p=son[p][a[i]],i++)
{
if (!son[p][a[i]]) break;
if (i!=k) ans+=sum[son[p][a[i]]]; else ans+=size[son[p][a[i]]];
}
printf("%d\n",ans);
}
int main()
{
// freopen("sec.in","r",stdin);
// freopen("sec.out","w",stdout);
int k;
read(n); read(m); tot=;
for (int i=; i<=n; i++)
{
read(k); for (int j=; j<=k; j++) read(a[j]);
insert(k);
}
for (int i=; i<=m; i++)
{
read(k);
for (int j=; j<=k; j++) read(a[j]);
workout(k);
}
return ;
}

[Usaco2008 Dec]Secret Message 秘密信息的更多相关文章

  1. 1590: [Usaco2008 Dec]Secret Message 秘密信息

    1590: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 5 Sec  Memory Limit: 32 MBSubmit: 209  Solved:  ...

  2. bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息

    1590: [Usaco2008 Dec]Secret Message 秘密信息 Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共 ...

  3. BZOJ1590:[Usaco2008 Dec]Secret Message秘密信息

    浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ...

  4. BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息

    建立一颗trie树,记录下来每个点以它为结尾的字符串的个数cnt,和它的子树内有多少字符串size 于是查询的时候就只需要把沿途的cnt加起来,再加上最后的size就好了 /************* ...

  5. [BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)

    传送门 看到前缀就要想到字典树! 看到前缀就要想到字典树! 看到前缀就要想到字典树! #include <cstdio> #include <iostream> #define ...

  6. 【Trie】Secret Message 秘密信息

    [题目链接]: https://loj.ac/problem/10054 [题意] 我认为这个题目最难的是题意: 其实分了两种情况: 1.如果当前文本串匹配不完,那么答案的是:匹配过程中遇到的模式串结 ...

  7. 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]

    洛谷传送门,BZOJ传送门 秘密消息Secret Message Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共有M(1≤M≤5 ...

  8. [USACO08DEC] 秘密消息Secret Message

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

  9. 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机

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

随机推荐

  1. List<Map>去重排序

    数据格式 [ { "id":"d3e8a9d6-e4c6-4dd8-a94f-07733d3c1b59", "parentId":" ...

  2. php的header函数之设置content-type

    //定义编码 header( 'Content-Type:text/html;charset=utf-8 '); //Atom header('Content-type: application/at ...

  3. 【转载】linux环境下为firefox/chrome浏览器安装flash player

    本文转载自 http://blog.sina.com.cn/s/blog_6ad624380102v1xf.html     firefox安装flash player的方法: 先到adobe网站上下 ...

  4. HDU 5531 Rebuild

    2015 ACM/ICPC 长春现场赛 E题 三分. 如果节点个数是奇数,那么直接列方程可以求解,因为,如果第一个圆半径变大,必然导致最后一个圆的半径变大, 所以,节点是奇数的时候,要么无解,要么只有 ...

  5. dotnet webservice处理数据量过大,ajax请求返回500错误解决方案

    ajax请求webservice返回json数据,数据规模过大时ajax请求会得到500的响应,webservice+ajax处理大规模的数据需要在web.config中进行如下配置: <sys ...

  6. IFieldEdit Interface 接口

    Description The IFieldEdit interface is used when creating new fields. You should not use it to modi ...

  7. 强制修改mysql 中root的密码

    /etc/init.d/mysqld stop   (service mysqld stop )/usr/bin/mysqld_safe --skip-grant-tables另外开个SSH连接[ro ...

  8. fixed定位与absolute定位

    相同点: 1.fixed定位和absolute定位都是绝对定位 2.fixed定位和absolute定位都脱离了标准文档流, 3.未设置偏移量时,都定位在父元素的左上角 tip:元素设置相对定位或绝对 ...

  9. (中等) CF 555E Case of Computer Network,双连通+树。

    Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible a ...

  10. 转 [ javascript面向对象技术

    以下文章来自iteye,作者是 sdcyst ,个人主页 http://www.iteye.com/topic/288813 类变量/类方法/实例变量/实例方法先补充一下以前写过的方法:在javasc ...