建立一颗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 <cstdio>
  11.  
  12. using namespace std;
  13.  
  14. inline int read();
  15.  
  16. struct trie {
  17. trie *son[];
  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[] = c -> son[] = NULL;
  26. c -> sz = , c -> tail = ;
  27. return c++;
  28. }
  29. #undef Len
  30.  
  31. void insert(int x) {
  32. ++this -> sz;
  33. if (x == ) {
  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 - );
  40. }
  41.  
  42. int query(int x) {
  43. if (x == ) 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 - ) + 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 = ; i <= n; ++i) t -> insert(read());
  60. for (i = ; i <= m; ++i) printf("%d\n", t -> query(read()));
  61. return ;
  62. }
  63.  
  64. inline int read() {
  65. static int x;
  66. static char ch;
  67. x = , ch = getchar();
  68. while (ch < '' || '' < ch)
  69. ch = getchar();
  70. while ('' <= ch && ch <= '') {
  71. x = x * + ch - '';
  72. ch = getchar();
  73. }
  74. return x;
  75. }

BZOJ1590 [Usaco2008 Dec]Secret Message 秘密信息的更多相关文章

  1. [BZOJ1590] [Usaco2008 Dec]Secret Message 秘密信息(字典树)

    传送门 看到前缀就要想到字典树! 看到前缀就要想到字典树! 看到前缀就要想到字典树! #include <cstdio> #include <iostream> #define ...

  2. [Usaco2008 Dec]Secret Message 秘密信息

    2794: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 7  Solved: 3 ...

  3. 1590: [Usaco2008 Dec]Secret Message 秘密信息

    1590: [Usaco2008 Dec]Secret Message 秘密信息 Time Limit: 5 Sec  Memory Limit: 32 MBSubmit: 209  Solved:  ...

  4. bzoj 1590: [Usaco2008 Dec]Secret Message 秘密信息

    1590: [Usaco2008 Dec]Secret Message 秘密信息 Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共 ...

  5. BZOJ1590:[Usaco2008 Dec]Secret Message秘密信息

    浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://lydsy.com/JudgeOnline/problem ...

  6. 【Trie】Secret Message 秘密信息

    [题目链接]: https://loj.ac/problem/10054 [题意] 我认为这个题目最难的是题意: 其实分了两种情况: 1.如果当前文本串匹配不完,那么答案的是:匹配过程中遇到的模式串结 ...

  7. 洛谷P2922 [USACO008DEC] 秘密消息Secret Message [Trie树]

    洛谷传送门,BZOJ传送门 秘密消息Secret Message Description     贝茜正在领导奶牛们逃跑.为了联络,奶牛们互相发送秘密信息.     信息是二进制的,共有M(1≤M≤5 ...

  8. [USACO08DEC] 秘密消息Secret Message

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

  9. 「USACO08DEC」「LuoguP2922」秘密消息Secret Message(AC自动机

    题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret bin ...

随机推荐

  1. factory工厂模式之工厂方法FactoryMethod

    工厂方法(Factory Method) * 工厂方法把不同的产品放在实现了工厂接口的不同工厂类(FactoryAImpl,FactoryBImpl...)里面, * 这样就算其中一个工厂类出了问题, ...

  2. Jquery基本、层次选择器

    基本选择器: $("#none").css("background","#bbffaa"); 改变id为none的所有元素的背景色 $(&q ...

  3. Scrum Meeting---Ten(2015-11-5)

    今日已完成任务和明日要做的任务 姓名 今日已完成任务 今日时间 明日计划完成任务 估计用时 董元财 分类页设计 4h 商品详单设计 4h 胡亚坤 首页设计 2h 滚动广告栏设计 2h 刘猛 服务器测试 ...

  4. 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 ...

  5. openstack 网卡

    桥接基本原理: 物理网卡eth0 br0(桥) tap0,tap1(tap是给vm使用的接口)

  6. iOS - OC NSString 字符串

    前言 @interface NSString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> @interface NSM ...

  7. DBCP JAVA 连接池

    package com.sinoglobal.db; import java.sql.Connection; import java.sql.DriverManager; import java.sq ...

  8. OC拓展(category)

    1. 扩展类的功能Category提供了一种比继承(inheritance)更为简洁的方法来对class进行扩展,我们可以为任何已经存在的class添加方法(不包括数据成员)却不需要访问该class的 ...

  9. img、input到底是行内还是块级元素?

    一.img.input属于行内替换元素.height/width/padding/margin均可用.效果等于块元素.      行内非替换元素,例如, height/width/padding to ...

  10. [js] 函数节流

    原文链接:http://www.alloyteam.com/2012/11/javascript-throttle/