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 ...
随机推荐
- 编写pl/sql时,报错
/* 写一个简单的PL/SQL */ declare a ; b ; c number; begin c:=(a+b)/(a-b); dbms_output.put_line(c); exceptio ...
- CodeForces 724G: Xor-matic Number of the Graph
题目传送门:CF724G. 题意简述: 一张 \(n\) 个点的无向图,边有边权. 定义三元组 \((u,v,w)(1\le u < v\le n)\) 合法当且仅当存在从点 \(u\) 到点 ...
- Floyd_Warshall算法
Floyd_Warshall算法主要用于求解所有节点对的最短路径,代码如下: #include<iostream> using namespace std; #define Inf 655 ...
- APUE-文件和目录(二)函数access,mask,chmod和粘着位
4.7 函数access和faccessat 当一个进程使用了设置用户ID和设置组ID作为另一个用户(或者组)运行时,这时候有效用户(组)ID和实际用户(组)ID不一样,但进程仍然希望测试实际用户(组 ...
- [问题解决]同时显示多个Notification时PendingIntent的Intent被覆盖?
情况是这样的,使用NotificationManager触发多个Notification: private Notification genreNotification(Context context ...
- Centos 软连接和硬链接
1.软链接: 建立软链接:ln -s /usr/local/node-v4.2.6-linux-x86/bin/node /usr/local/bin/node 解释:将/usr/local/node ...
- find命令的使用
在以.conf结尾的文件里面查找含有aaa字符串的那一行 ( -name后面可以写 "*.*" 即匹配所有的文件 ) find / -name "*.conf& ...
- Java Http接口加签、验签操作方法
1.业务背景 最近接触了一些电商业务,发现在处理电商业务接口时,比如淘宝.支付类接口,接口双方为了确保数据参数在传输过程中未经过篡改,都需要对接口数据进行加签,然后在接口服务器端对接口参数进行验签,确 ...
- POJ 2516 Minimum Cost(拆点+KM完备匹配)
题目链接:http://poj.org/problem?id=2516 题目大意: 第一行是N,M,K 接下来N行:第i行有K个数字表示第i个卖场对K种商品的需求情况 接下来M行:第j行有K个数字表示 ...
- [USACO18FEB]Snow Boots S
提供一种无脑DP做法 题目中大概有这么些东西:位置,穿鞋,跑路 数据小,那么暴力开数组暴力DP吧 设dp[i][j]表示穿着鞋子j,到达位置i是否可行 无脑转移 枚举位置,正在穿哪双鞋,换成哪双走出去 ...