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 ...
随机推荐
- [转]Ubuntu下ROS开发环境搭建(QT+ros_qtc_plugin)
ROS与C++入门教程-搭建开发环境(QT+ros_qtc_plugin) PS : 在“安装ros_qtc_plugin插件”这一步中,原文提到“ Ubuntu 14.04使用apt-get方式安装 ...
- 一个罕见的MSSQL注入漏洞案例
一个罕见的MSSQL注入漏洞案例 这里作者准备分享一个在去年Google赏金计划中发现的相当罕见漏洞,也是作者在整个渗透测试生涯中唯一一次遇到的. 目标网站使用了微软 SQL Server 数据库并且 ...
- oracle同义词是什么意思?
相当于alias,比如把user1.table1 在user2中建一个同义词table1create synonym table1 for user1.table1;这样当我们在user2中查sele ...
- 理解mipi协议【转】
转自:http://blog.csdn.net/wanglining1987/article/details/50202615 完成mipi信号通道分配后,需要生成与物理层对接的时序.同步信号: MI ...
- 芒果TV 视频真实的地址获取
# coding=utf-8 import requests import json import re import os import urlparse import random vid = r ...
- 在windows中安装两个不同版本的Python
这段时间买了一本 利用Python进行数据分析的书.书上要我将原来安装的Python的环境去掉,但是我觉得这样做不行,我以前写过的很多东西还在呢.遂在博客中找到了解决方法,记录之. 首先,我们安装了两 ...
- 用户说体验 | 关于阿里百川HotFix你需要了解的一些细节
最近很火的热修复技术,无意中了解到阿里百川也在做,而且Android.iOS两端都支持,所以决定试一试.试用一段时间后,感觉还不错,主要是他们有一个团队在不断维护更新这个产品,可以看到他们的版本更新记 ...
- 编程六月定律 | 外刊IT评论网
编程六月定律 上周,我被迫对一个很老的项目做一些修改.麻烦是,当开始着手时,我真的记不清这个项目究竟有多老了. 这实际上是我使用Codeigniter实现的第一个MVC项目.打开项目文件后,很多东西都 ...
- Pg188-3 构造方法
package org.hanqi.array; public class Father { private String name; private int age; public String g ...
- Pg188-2 覆盖 向上转型
package org.hanqi.array; public class DongWu { private String name; private String color; public Str ...