[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 ...
随机推荐
- openwrt 中route配置
route配置项默认保存在文件 /etc/config/network 中. 配置route的接口“interface” 使用的协议需要为dhcp才可. config interface 'wan' ...
- win7设置固定IP
正文: 你必须知道你的路由器网关,一般是192.168.1.1(或192.168.0.1) 按传统的来:开始——控制面板——网络和共享中心——更改适配器设置.一般来讲,这里应该有两个图标,一个是有线网 ...
- Eclipse与github整合
Eclipse与github整合 Windows系统下: github官方指南:https://help.github.com/articles/set-up-git Git?是个正快速成长的版本控制 ...
- 配置_DruidDataSource参考配置
配置_DruidDataSource参考配置 <!-- 数据库驱动 --> <property name="driverClassName" value=&quo ...
- Webpack中的css-loader 和style-loader
传统上我们会在html文件中引入CSS代码,借助webpack style-loader和css-loader我们可以在.js文件中引入css文件并让样式生效. style-loader和css-lo ...
- 【linux】crontab失效
在linux上,crontab任务全部使用完整路径,但是任务无效. 检测crontab 服务是否启动, /etc/init.d/cron status /etc/init.d/cron restart
- MySQL 由 5.7 升级为 8.0 之后,Laravel 的配置改动
开发机上升级了 MySQL 8.0, 原有的 Laravel 5.5 项目就启动失败了. 报错信息是: [2018-05-30 11:17:37] local.ERROR: SQLSTATE[4200 ...
- WebService简介-02
WebService-面向服务编程SOA WebService-远程通信 运行效果: 1:添加服务器引用http://www.webxml.com.cn/WebServices/WeatherWebS ...
- 跨域资源共享CORS
CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpRequest请求,从而 ...
- #7 //[CQOI2014]和谐矩阵
题解: bitset优化高斯消元 无关变量为1 #include <bits/stdc++.h> using namespace std; #define eps 1e-9 #define ...