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 ...
随机推荐
- factory工厂模式之工厂方法FactoryMethod
工厂方法(Factory Method) * 工厂方法把不同的产品放在实现了工厂接口的不同工厂类(FactoryAImpl,FactoryBImpl...)里面, * 这样就算其中一个工厂类出了问题, ...
- Jquery基本、层次选择器
基本选择器: $("#none").css("background","#bbffaa"); 改变id为none的所有元素的背景色 $(&q ...
- Scrum Meeting---Ten(2015-11-5)
今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 分类页设计 4h 商品详单设计 4h 胡亚坤 首页设计 2h 滚动广告栏设计 2h 刘猛 服务器测试 ...
- ubuntu linux 使用常见问题
Q:gedit不支持windows下的中文显示 A:http://wiki.ubuntu.org.cn/Gedit%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81 Q:bash ...
- openstack 网卡
桥接基本原理: 物理网卡eth0 br0(桥) tap0,tap1(tap是给vm使用的接口)
- iOS - OC NSString 字符串
前言 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> @interface NSM ...
- DBCP JAVA 连接池
package com.sinoglobal.db; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...
- OC拓展(category)
1. 扩展类的功能Category提供了一种比继承(inheritance)更为简洁的方法来对class进行扩展,我们可以为任何已经存在的class添加方法(不包括数据成员)却不需要访问该class的 ...
- img、input到底是行内还是块级元素?
一.img.input属于行内替换元素.height/width/padding/margin均可用.效果等于块元素. 行内非替换元素,例如, height/width/padding to ...
- [js] 函数节流
原文链接:http://www.alloyteam.com/2012/11/javascript-throttle/