【BZOJ】1590: [Usaco2008 Dec]Secret Message 秘密信息
来源:互联网 发布:windows内核编程 书籍 编辑:程序博客网 时间:2024/05/22 03:25
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.
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
3
1
1
2
题解:
字典树,找前缀,记录位置
#include <cstdio> #include <cstring> const int MAXN=500001;struct node { int s; int link[2]; }t[MAXN]; int ans; int nt; int n,m; int sum[MAXN];void add(int len) { int x; int ansm=0; int p=0; for(int i=1;i<=len;i++) { scanf("%d",&x); int zz=x; if(t[p].link[zz]==0) t[p].link[zz]=++nt; p=t[p].link[zz]; sum[p]++; } t[p].s++; } int Query(int len){ int x; int ans=0; int p=0; for(int i=1;i<=len;i++) { scanf("%d",&x); int zz=x; if(!t[p].link[zz]) { for(i=i+1;i<=len;i++) scanf("%d",&x); return ans; } p=t[p].link[x]; ans+=t[p].s; } return ans-t[p].s+sum[p];}int main() { char z[100]=""; scanf("%d%d",&n,&m); int x; for(int i=1;i<=n;i++) { scanf("%d",&x); add(x); } for(int i=1;i<=m;i++) { scanf("%d",&x),printf("%d\n",Query(x)); }}
0 0
- 【BZOJ】1590: [Usaco2008 Dec]Secret Message 秘密信息
- BZOJ 1590: [Usaco2008 Dec]Secret Message 秘密信息 Trie树
- [Usaco2008 Dec]Secret Message 秘密信息
- BZOJ1590: [Usaco2008 Dec]Secret Message 秘密信息
- BZOJ1590: [Usaco2008 Dec]Secret Message 秘密信息 Trie树
- bzoj 1590 luogu2292 [USACO08DEC]秘密消息Secret Message(题解)
- BZOJ 2606 USACO 2008 Dec Gold 2.Secret Message 字典树
- P2922 [USACO08DEC]秘密消息Secret Message
- BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草
- BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
- BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草
- BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
- BZOJ 1607([Usaco2008 Dec]Patting Heads 轻拍牛头-线性筛)
- BZOJ-1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法+乱搞
- bzoj 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 暴力
- 筛-BZOJ-1607-[Usaco2008 Dec]Patting Heads 轻拍牛头
- bzoj 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
- [bzoj 1607] [Usaco2008 Dec]Patting Heads 轻拍牛头 筛数
- 基于qualcomm平台的kincet应用系列一之Turtlebot(自动跟随机器人)上篇
- hdu2048
- 爬虫07 爬取阿里旅行特价机票
- 阿里云centos 6.0+安装mysql 5.6,并开启远程连接
- 常用js
- 【BZOJ】1590: [Usaco2008 Dec]Secret Message 秘密信息
- POJ 1125 Stockbroker Grapevine (最短路 floyd)
- 逐帧动画FrameAnimation和汤姆猫小例子
- python2.7 threading 模块 三 Condition Semaphore
- String类用法总结
- 替换空格
- caffe的caffe.proto
- 将链表中的所有元素为奇数的节点移到元素为偶数节点的前面,并使奇数之间顺序反转,偶数之间顺序反转
- FFmpeg在Linux下安装编译过程