bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息
1590: [Usaco2008 Dec]Secret Message 秘密信息
Description
Input
Output
Sample Input
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
3
1
1
2
HINT
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
Source
题解:
比较裸的trie树,用p[x].cnt表示在x结尾的串的个数,这样只能算出长度不大于当前串的方案数,所以再设p[x].sum表示x子树的和。
总的方案就是路径上的cnt累加再加上sum
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
struct node
{
int Next[2],cnt,sum;
void init()
{
cnt=sum=0;
memset(Next,-1,sizeof(Next));
}
}p[500005];
int n,m,i,j,x,a[10005],b[10005],tot;
inline void read(int&a){char c;while(!(((c=getchar())>='0')&&(c<='9')));a=c-'0';while(((c=getchar())>='0')&&(c<='9'))(a*=10)+=c-'0';}
void update(int len)
{
int x=0,k=0;
for(int i=1;i<=len;i++)
{
int j=a[i];
if(p[x].Next[j]==-1)
{
p[x].Next[j]=++tot;
p[tot].init();
}
x=p[x].Next[j];b[++k]=x;
}
p[x].cnt++;
for(int i=1;i<k;i++) p[b[i]].sum++;
}
int solve(int len)
{
int x=0,ans=0;
for(int i=1;i<=len;i++)
{
int j=a[i];
if(p[x].Next[j]==-1) return ans;
x=p[x].Next[j];
ans+=p[x].cnt;
}
return ans+p[x].sum;
}
int main()
{
read(n),read(m);
p[0].init();
for(i=1;i<=n;i++)
{
read(x);
for(j=1;j<=x;j++) read(a[j]);
update(x);
}
for(i=1;i<=m;i++)
{
read(x);
for(j=1;j<=x;j++) read(a[j]);
printf("%d\n",solve(x));
}
return 0;
}
bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息的更多相关文章
- 1590: [Usaco2008 Dec]Secret Message 秘密信息
		1590: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 5 Sec Memory Limit: 32 MBSubmit: 209 Solved: ... 
- [Usaco2008 Dec]Secret Message 秘密信息
		2794: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3 ... 
- BZOJ1590:[Usaco2008 Dec]Secret Message秘密信息
		浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ... 
- BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息
		建立一颗trie树,记录下来每个点以它为结尾的字符串的个数cnt,和它的子树内有多少字符串size 于是查询的时候就只需要把沿途的cnt加起来,再加上最后的size就好了 /************* ... 
- [BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)
		传送门 看到前缀就要想到字典树! 看到前缀就要想到字典树! 看到前缀就要想到字典树! #include <cstdio> #include <iostream> #define ... 
- 【Trie】Secret Message 秘密信息
		[题目链接]: https://loj.ac/problem/10054 [题意] 我认为这个题目最难的是题意: 其实分了两种情况: 1.如果当前文本串匹配不完,那么答案的是:匹配过程中遇到的模式串结 ... 
- BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法
		1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lyds ... 
- BZOJ 1606: [Usaco2008 Dec]Hay For Sale 购买干草( dp )
		-------------------------------------------------------------------- #include<cstdio> #include ... 
- BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
		1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤10 ... 
随机推荐
- [转]QList内存释放
			QList<T> 的释放分两种情况: 1.T的类型为非指针,这时候直接调用clear()方法就可以释放了,看如下测试代码 #include <QtCore/QCoreApplicat ... 
- Count of Smaller Number before itself
			Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ... 
- C++学习之路(十一):C++的初始化列表
			结论: 1.在C++中,成员变量的初始化顺序与变量在类型中的声明顺序相同,而与他们在构造函数的初始化列表中的顺序无关. 2.构造函数分为两个阶段执行:1)初始化阶段:2)普通的计算阶段,表现为赋值操作 ... 
- MODULE_DEVICE_TABLE (二)【转】
			转自:http://blog.csdn.net/uruita/article/details/7263290 1. MODULE_DEVICE_TABLE (usb, skel_table);该宏生成 ... 
- Go 的package
			一.包的一些基本的概念 1.在同一个目录下的所有go文件中,只能有一个main函数.如果存在多个main函数,则在编译的时候会报错 那么,在同一个目录下的两个go文件究竟是什么关系? 为什么会彼此影响 ... 
- 20165203迭代和JDB测试
			1.使用C(n,m)=C(n-1,m-1)+C(n-1,m)公式进行递归编程实现求组合数C(m,n)的功能 public class C { public static void main(Strin ... 
- Docker镜像和容器
			本节内容: 安装Docker 卸载docker 镜像基本操作 容器基本操作 一.安装Docker Docker 对 Linux 内核版本的最低要求是3.10,如果内核版本低于 3.10 会缺少一些运行 ... 
- deploy.sh
			备份一下之前的一个脚本吧 #!bin/bash adb uninstall org.cocos2d.fishingjoy4 for apk in `find . -name '*.apk' | xar ... 
- bzoj 1264 [AHOI2006]基因匹配Match  dp + 树状数组
			思路:好难想啊, 考虑到应该从每个数字只有5个数字下手, 但是不知道到底该怎么写.. 首先我们将第一个串按数字的种类分类, 每一类里面有5个, 然后将第二个串里面的数字一个一个加,如果一个加入的第 i ... 
- linux下文件转码
			一.工具介绍 enca是一个很好用的文件转码工具,使用命令 sudo apt-get install enca 即可安装 二.基本用法 1.查看文件编码 $ enca filename 2.文件转码 ... 
