[USACO08DEC]Secret Message
OJ题号:
洛谷2922
思路:
字典树,每个结点记录经过该节点的字符串数cnt和以该结点结尾的字符串数量val。
每次询问时累加经过节点的val值和结尾结点的cnt值。
#include<cstdio>
#include<cctype>
#include<cstring>
inline int getint() {
char ch;
while(!isdigit(ch=getchar()));
int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
class Trie {
private:
static const int maxnode=;
int ch[maxnode][];
int val[maxnode],cnt[maxnode];
int sz;
public:
Trie() {
sz=;
memset(ch,,sizeof ch);
memset(val,,sizeof val);
memset(cnt,,sizeof cnt);
}
void insert(int *s) {
int x=,n=s[];
for(int i=;i<=n;i++) {
if(!ch[x][s[i]]) {
ch[x][s[i]]=++sz;
}
cnt[x]++;
x=ch[x][s[i]];
}
val[x]++;
}
int query(int *s) {
int x=,n=s[],ans=;
for(int i=;i<=n;i++) {
ans+=val[x];
if(!ch[x][s[i]]) {
return ans;
}
x=ch[x][s[i]];
}
return ans+val[x]+cnt[x];
}
};
Trie t;
int main() {
int m=getint(),n=getint();
int s[];
while(m--) {
s[]=getint();
for(int i=;i<=s[];i++) s[i]=getint();
t.insert(s);
}
while(n--) {
s[]=getint();
for(int i=;i<=s[];i++) s[i]=getint();
printf("%d\n",t.query(s));
}
return ;
}
[USACO08DEC]Secret Message的更多相关文章
- 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
- 洛谷p2922[USACO08DEC]秘密消息Secret Message
题目: 题目链接:[USACO08DEC]秘密消息Secret Message 题意: 给定n条01信息和m条01密码,对于每一条密码A,求所有信息中包含它的信息条数和被它包含的信息条数的和. 分析: ...
- 2078 Problem H Secret Message 中石油-未提交-->已提交
题目描述 Jack and Jill developed a special encryption method, so they can enjoy conversations without wo ...
- [Usaco2008 Dec]Secret Message 秘密信息
2794: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 7 Solved: 3 ...
- 1590: [Usaco2008 Dec]Secret Message 秘密信息
1590: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 5 Sec Memory Limit: 32 MBSubmit: 209 Solved: ...
- Secret Message ---- (Trie树应用)
Secret Message 总时间限制: 2000ms 内存限制: 32768kB 描述 Bessie is leading the cows in an attempt to escap ...
- bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息
1590: [Usaco2008 Dec]Secret Message 秘密信息 Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共 ...
- 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]
洛谷传送门,BZOJ传送门 秘密消息Secret Message Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共有M(1≤M≤5 ...
- [USACO08DEC] 秘密消息Secret Message
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
随机推荐
- c# 取本地ip地址
public static System.Net.IPAddress[] GetIpAddress() { string hostName = System.Net.Dns.GetHostName() ...
- xpath与nodejs解析xml
测试xpath的工具 http://www.freeformatter.com/xpath-tester.html#ad-output http://www.xpathtester.com/test ...
- 记录entityframework生成的sql语句
Interceptors (EF6.1 Onwards) Starting with EF6.1 you can register interceptors in the config file. I ...
- 中文汉字和常见英文数字等的unicode编码范围实例页面
链接: https://www.zhangxinxu.com/study/201611/chinese-language-unicode-range.html
- Mess it up!搞乱代码
Mess it up! 作者: Laruence( ) 本文地址: http://www.laruence.com/2009/01/07/656.html 转载请注明出处 恩,这段代码还能再乱点, ...
- vue构建项目全过程
1.node版本请更新到6.9.X版本以上,不然npm依赖会出问题 2.命令行里运行npm install --global vue-cli 3.npm install --global webpac ...
- CentOS中在/etc/rc.local添加开机自启动项启动失败
应项目要求需要在开机的时候启动自己的Agent程序,想当然的直接就往/etc/rc.local当中添加启动命令,结果重启之后发现什么都没有发生....一开始还以为是Python路径的问题,结果改成绝对 ...
- Deep Learning系统实训之一:深度学习基础知识
K-近邻与交叉验证 1 选取超参数的正确方法是:将原始训练集分为训练集和验证集,我们在验证集上尝试不同的超参数,最后保留表现最好的那个. 2 如果训练数据量不够,使用交叉验证法,它能帮助我们在选取最优 ...
- cf276E 两棵线段树分别维护dfs序和bfs序,好题回头再做
搞了一晚上,错了,以后回头再来看 /* 对于每次更新,先处理其儿子方向,再处理其父亲方向 处理父亲方向时无法达到根,那么直接更新 如果能达到根,那么到兄弟链中去更新,使用bfs序 最后,查询结点v的结 ...
- 性能测试一:jmeter基础入门
JMeter,一个100%的纯Java桌面应用,由Apache组织的开放源代码项目,它是功能和性能测试的工具.具有高可扩展性.支持Web(HTTP/HTTPS).SOAP.FTP.JAVA等多种协议的 ...