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 ...
随机推荐
- Visual C#两分钟搭建BHO IE钩子(转)
摘自:http://www.cnblogs.com/mvc2014/p/3776054.html 微软在1997年正式推出Browser Helper Object (BHO), 使程序员能够更好的对 ...
- Codeforces Round #376 (Div. 2) C. Socks bfs
C. Socks time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- android的简单入门学习
话说光配环境就整死我了, 不是说多么难, 是最近google被屏了, 很多sdk里面需要下载的东西都下不下来, 坑爹啊. 最后跟扫拉稀要了一个他配置好的,才运行了. android目录分析: ass ...
- 妙用||和&&
&&和||在JQuery源代码内尤为使用广泛,由于本人没有系统的学习js,所以只能粗略的自我理解出来,希望大家指点下.粗略理解如下: a() && b() :如果执行a ...
- asp.netMVC中,视图层和控制器层的传值
Asp.Net Mvc 控制器与视图的数据传递 摘要:本文将讨论asp.net mvc框架中的数据传递. 数据传递也就是控制器和视图之间的交互,比如在视图中提交的数据,在控制器怎么获取,或者控制器从业 ...
- 用python实现把数字人民币金额转换成大写的脚本程序
# -*- coding: utf-8 -*- def Num2MoneyFormat( change_number ): """ .转换数字为大写货币格式( forma ...
- cublas相关的知识
下面链接给出了一个例子,怎么用cublas进行矩阵的运算提速,也说明了cublas的大致的使用方法. http://www.cnblogs.com/scut-fm/p/3756242.html cub ...
- JavaWeb 5 Tomcat
5 Tomcat 1 Web开发入门 1.1 引入 之前的程序: java桌面程序,控制台控制,socket gui界面.javase规范 ...
- h5 input file ajax实现文件上传
<input type="file" accept="image/*" height="0" class="file_inp ...
- 从linux系统mysql导出数据库
原文:http://blog.csdn.net/lifuxiangcaohui/article/details/50763674 1.MySQL数据库导出 /usr/local/mysql/bin/m ...