博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息
阅读量:4516 次
发布时间:2019-06-08

本文共 1961 字,大约阅读时间需要 6 分钟。

建立一颗trie树,记录下来每个点以它为结尾的字符串的个数cnt,和它的子树内有多少字符串size

于是查询的时候就只需要把沿途的cnt加起来,再加上最后的size就好了

 

1 /************************************************************** 2     Problem: 1590 3     User: rausen 4     Language: C++ 5     Result: Accepted 6     Time:320 ms 7     Memory:3892 kb 8 ****************************************************************/ 9  10 #include 
11 12 using namespace std;13 14 inline int read();15 16 struct trie {17 trie *son[2];18 int sz, tail;19 20 #define Len (1 << 16)21 void* operator new(size_t) {22 static trie *mempool, *c;23 if (c == mempool)24 mempool = (c = new trie[Len]) + Len;25 c -> son[0] = c -> son[1] = NULL;26 c -> sz = 0, c -> tail = 0;27 return c++;28 }29 #undef Len30 31 void insert(int x) {32 ++this -> sz;33 if (x == 0) {34 ++this -> tail;35 return;36 }37 int t = read();38 if (!this -> son[t]) this -> son[t] = new()trie;39 this -> son[t] -> insert(x - 1);40 }41 42 int query(int x) {43 if (x == 0) return this -> sz;44 int t = read();45 if (!this -> son[t]) {46 while (--x) read();47 return this -> tail;48 }49 return this -> son[t] -> query(x - 1) + this -> tail;50 }51 } *t;52 53 int n, m;54 55 int main() {56 int i;57 n = read(), m = read();58 t = new()trie;59 for (i = 1; i <= n; ++i) t -> insert(read());60 for (i = 1; i <= m; ++i) printf("%d\n", t -> query(read()));61 return 0;62 }63 64 inline int read() {65 static int x;66 static char ch;67 x = 0, ch = getchar();68 while (ch < '0' || '9' < ch)69 ch = getchar();70 while ('0' <= ch && ch <= '9') {71 x = x * 10 + ch - '0';72 ch = getchar();73 }74 return x;75 }
View Code

 

转载于:https://www.cnblogs.com/rausen/p/4456939.html

你可能感兴趣的文章
SQL语句创建函数
查看>>
解决mysql无法显示中文/MySQL中文乱码问号等问题
查看>>
CentOS 7.2 配置mysql5.7
查看>>
python输出转义字符
查看>>
java基础43 IO流技术(输入字节流/缓冲输入字节流)
查看>>
计算一个整数二进制中1的个数
查看>>
netdom join 错误:指定的域不存在,或无法联系。
查看>>
Android中Dialog的使用
查看>>
Android Activity接收Service发送的广播
查看>>
[Leetcode] Spiral Matrix | 把一个2D matrix用螺旋方式打印
查看>>
加速和监控国际网络
查看>>
【Flex】读取本地XML,然后XML数据转成JSON数据
查看>>
字符串循环右移-c语言
查看>>
解决从pl/sql查看oracle的number(19)类型数据为科学计数法的有关问题
查看>>
古训《增广贤文》
查看>>
职场的真相——七句话
查看>>
xcode命令行编译时:codesign命令,抛出“User interaction is not allowed.”异常 的处理...
查看>>
[转载]开机出现A disk read error occurred错误
查看>>
STM32 C++编程 002 GPIO类
查看>>
无线冲方案 MCU vs SoC
查看>>