[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 ...
随机推荐
- 查看Windows版本号
1.Win+R,在运行中输入:msinfo32 2.在cmd中输入:ver 3.注册表(regedit)中:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows ...
- Linux内存管理2---段机制
1.前言 本文所述关于内存管理的系列文章主要是对陈莉君老师所讲述的内存管理知识讲座的整理. 本讲座主要分三个主题展开对内存管理进行讲解:内存管理的硬件基础.虚拟地址空间的管理.物理地址空间的管理. 本 ...
- ES系列一、CentOS7安装ES 6.3.1、集成IK分词器
Elasticsearch 6.3.1 地址: wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3. ...
- JS禁止鼠标右键、禁止全选、复制、粘贴的方法(所谓的防盗功能)
简述:一个防君子不防小人的鸡肋的功能,针对小白还行. 代码如下: <script> //都能支持 document.oncontextmenu = function (e) { retur ...
- shell脚本中冒号
格式:: your comment here 格式:# your comment here 写代码注释(单行注释). 例如: 格式:: 'comment line1 comment line2 mor ...
- iframe传递参数问题
在页面中嵌入了iframe,如果需要传递参数到iframe中 1.通过将参数嵌入到url中,在iframe中使用${param.xxx}可以获取 2.通过将参数存入到session中,在iframe中 ...
- 关于XMLHttpRequest状态的讨论及处理方法
今天主要是讨论下XMLHttpRequest的响应状态问题.我们知道,XMLHttpRequest的响应阶段有5个,分别是: 请求未初始化 服务器连接已建立 请求已接收 请求处理中 请求已完成,且响应 ...
- javascript 练习题目答案
//廖雪峰的博客的这个教程的答案//https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb300 ...
- Android 颜色透明度换算
每次开发的时候,UI在设计图中标注的颜色都是类似于#FF0000(红色),这倒没什么,但是呢后面却标注了30%的透明度,这下抓狂了,透明度怎么计算?不用着急,不用你算,收藏我这篇文章即可. 颜色简介 ...
- Oracle ____Undo
什么是回滚和撤销 update emp set sal=4000 where empno=7788 语句执行过程 1 检查empno=7788记录是否在buffer cache ,如果不存在,则读取到 ...