BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息
建立一颗trie树,记录下来每个点以它为结尾的字符串的个数cnt,和它的子树内有多少字符串size
于是查询的时候就只需要把沿途的cnt加起来,再加上最后的size就好了
/**************************************************************
Problem: 1590
User: rausen
Language: C++
Result: Accepted
Time:320 ms
Memory:3892 kb
****************************************************************/ #include <cstdio> using namespace std; inline int read(); struct trie {
trie *son[];
int sz, tail; #define Len (1 << 16)
void* operator new(size_t) {
static trie *mempool, *c;
if (c == mempool)
mempool = (c = new trie[Len]) + Len;
c -> son[] = c -> son[] = NULL;
c -> sz = , c -> tail = ;
return c++;
}
#undef Len void insert(int x) {
++this -> sz;
if (x == ) {
++this -> tail;
return;
}
int t = read();
if (!this -> son[t]) this -> son[t] = new()trie;
this -> son[t] -> insert(x - );
} int query(int x) {
if (x == ) return this -> sz;
int t = read();
if (!this -> son[t]) {
while (--x) read();
return this -> tail;
}
return this -> son[t] -> query(x - ) + this -> tail;
}
} *t; int n, m; int main() {
int i;
n = read(), m = read();
t = new()trie;
for (i = ; i <= n; ++i) t -> insert(read());
for (i = ; i <= m; ++i) printf("%d\n", t -> query(read()));
return ;
} inline int read() {
static int x;
static char ch;
x = , ch = getchar();
while (ch < '' || '' < ch)
ch = getchar();
while ('' <= ch && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x;
}
BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息的更多相关文章
- [BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)
传送门 看到前缀就要想到字典树! 看到前缀就要想到字典树! 看到前缀就要想到字典树! #include <cstdio> #include <iostream> #define ...
- [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: ...
- bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息
1590: [Usaco2008 Dec]Secret Message 秘密信息 Description 贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息. 信息是二进制的,共 ...
- BZOJ1590:[Usaco2008 Dec]Secret Message秘密信息
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ...
- 【Trie】Secret Message 秘密信息
[题目链接]: https://loj.ac/problem/10054 [题意] 我认为这个题目最难的是题意: 其实分了两种情况: 1.如果当前文本串匹配不完,那么答案的是:匹配过程中遇到的模式串结 ...
- 洛谷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 ...
- 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机
题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...
随机推荐
- 防止SQL注入问题
-----解决方案--------------------------------------------------------过滤URL中的一些特殊字符,动态SQL语句使用PrepareState ...
- struts2标签之列求和
struts2标签之列求和 <table width="100%" border="0" cellpadding="0" cellsp ...
- mybatis的xml文件中如何处理大小于号
在mybatis的xml配置文件中会遇到大小于号转化的问题,解决问题的方法如下: 1.用转义字符把>和<替换掉 SELECT * FROM test WHERE AND start_dat ...
- JavaWeb学习总结(十三)--数据库连接池
一.数据库连接池的概念 用池来管理Connection,这可以重复使用Connection.有了池,所以我们就不用自己来创建Connection,而是通过池来获取Connection对象.当使用完Co ...
- Git基本交互流程图
- ps前端切图常用快捷键
一.新建文件:预设-自定:宽度1920,单位像素,高度自定义:分辨率:72像素/英寸:颜色模式:RGB,8位:背景色:透明:可存储预设,下次新建的时候,直接在预设中,选择那个名称,点确定.视图:显示- ...
- B树索引
在SQL Server中,索引是一种增强式的存在,这意味着,即使没有索引,SQL Server仍然可以实现应有的功能.但索引可以在大多数情况下大大提升查询性能高.在OLAP中尤其明显,要完全理解索引的 ...
- 多路径(multi-path)安装测试实例
1.确保安装以下的包: device-mapper device-mapper-multipath [root@nticket1~]# rpm -qa "*device*" dev ...
- phpstorm xdebug 碰到很神奇的一件事
早上配置好了,且正常运行了,然后没有退出phpstorm, 结果到了下午,配置消失了, 还好稳住了阵脚,然后配置了,就好了. 很重要一点,在调试观看源代码的时候,一定要浅薄,然后深入,不然累死,得不偿 ...
- 测序深度和覆盖度(Sequencing depth and coverage)
总是跑数据,却对数据一无所知,这说不过去吧. 看几篇文章吧 Sequencing depth and coverage: key considerations in genomic analyses( ...