建立一颗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 秘密信息的更多相关文章

  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. Struts BaseAction工具类,封装Session,Request,Application,ModelDriven

    package com.ssh.shop.action; import java.io.InputStream; import java.lang.reflect.ParameterizedType; ...

  2. git学习笔记08-分支管理策略-实际上我们应该怎么应用分支

    Git用Fast forward模式(快进模式),但这种模式下,删除分支后,会丢掉分支信息. 如果要强制禁用Fast forward模式,Git就会在merge时生成一个新的commit,这样,从分支 ...

  3. JSP学习——原理

    JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术.   JSP这门技术的最大的特点在于,写jsp就像在写html,但它相比 ...

  4. [转载] 数据库分析手记 —— InnoDB锁机制分析

    作者:倪煜 InnoDB锁机制常常困扰大家,不同的条件下往往表现出不同的锁竞争,在实际工作中经常要分析各种锁超时.死锁的问题.本文通过不同条件下的实验,利用InnoDB系统给出的各种信息,分析了锁的工 ...

  5. Longest Common Prefix

    Description: Write a function to find the longest common prefix string amongst an array of strings.( ...

  6. git命令之git rebase 的用法

    rebase 假设你现在基于远程分支"origin",创建一个叫"mywork"的分支. $ git checkout -b mywork origin 现在我 ...

  7. hdu5823(反演dp)

    听说3^n也能水过去.. 其实应该是个经典题,求图染色这个np问题. 把问题拆成独立集来进行dp可以在3^n之内水过去. 拆成独立集的时候就发现,等价与一个经典的反演dp问题 然后复杂度就变成了 n* ...

  8. 实体类实现Parcelable(包含boolean类型)

    实体类实现Parcelable接口需要实现方法: public ExtSignClockEntity(Parcel in) { timeMess = in.readString(); repeatMe ...

  9. SSH命令行上传/下载文件

    上传:scp /path/file(这部分为本地的路径) user(远端目标用户名)@host(远端目标IP):/pathorfile(文件存储路径) 下载:scp user(远端用户名)@host( ...

  10. Activity UI Handler ProgressBar AsyncTask

    package io.vov.vitamio.activity; import android.app.Activity; import android.app.ProgressDialog; imp ...