bzoj 3439: Kpm的MC密码 Trie+动态开点线段树
题目大意:
题解:
首先我们发现这道题要查的是后缀不是前缀.
如果查前缀就可以迅速查找到字符串了不是吗hhhhh.
所以我们把所有的串倒过来
然后在每个节点上维护一颗线段树储存以它为根的子树中结尾的字符串的编号支持查找k大即可.
他们说这会T,但我跑的挺快的.
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 320010;
struct Edge{
int to,next;
}G[maxn<<1];
int head[maxn],cnt;
void add(int u,int v){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
}
int ch[maxn][30],a[maxn],nodecnt;
char s[maxn];
void insert(int id){
int n = strlen(s+1);
int nw = 0;
for(int i=n;i>=1;--i){
if(ch[nw][s[i] - 'a'] == 0){
ch[nw][s[i] - 'a'] = ++nodecnt;
nw = nodecnt;
}else nw = ch[nw][s[i] - 'a'];
}add(nw,id);
}
int ind[maxn],oud[maxn],dfs_clock;
#define v G[i].to
void dfs(int u){
int num = dfs_clock;
for(int i = head[u];i;i=G[i].next){
a[++dfs_clock] = v;
}
for(int i = 'a';i <= 'z';++i){
if(ch[u][i-'a'] != 0){
dfs(ch[u][i - 'a']);
}
}
for(int i = head[u];i;i=G[i].next) ind[v] = num,oud[v] = dfs_clock;
}
#undef v
struct Node{
Node *ch[2];
int num;
void update(){
num = ch[0]->num + ch[1]->num;
}
}*null,*T[maxn];
Node mem[maxn*30],*C;
inline void init(){
C = mem;null = C++;
null->ch[0] = null->ch[1] = null;
null->num = 0;T[0] = null;
}
Node* build(Node *rt,int l,int r,int pos){
Node *p = C++;*p = *rt;
if(l == r){
p->num ++ ;
return p;
}
int mid = l+r >> 1;
if(pos <= mid) p->ch[0] = build(rt->ch[0],l,mid,pos);
else p->ch[1] = build(rt->ch[1],mid+1,r,pos);
p->update();return p;
}
int query(Node *p1,Node *p2,int l,int r,int x){
while(l != r){
int mid = l+r >> 1;
int tmp = p2->ch[0]->num - p1->ch[0]->num;
if(x > tmp){
x -= tmp;
p1 = p1->ch[1];p2 = p2->ch[1];
l = mid+1;
}else{
p1 = p1->ch[0];p2 = p2->ch[0];
r = mid;
}
}return l;
}
int main(){
int n;read(n);
init();
for(int i=1;i<=n;++i){
scanf("%s",s+1);
insert(i);
}
dfs(0);
for(int i=1;i<=n;++i) T[i] = build(T[i-1],1,n,a[i]);
for(int i=1,x;i<=n;++i){
read(x);
if(oud[i] - ind[i] < x) puts("-1");
else printf("%d\n",query(T[ind[i]],T[oud[i]],1,n,x));
}
getchar();getchar();
return 0;
}
bzoj 3439: Kpm的MC密码 Trie+动态开点线段树的更多相关文章
- BZOJ 3439: Kpm的MC密码( trie + DFS序 + 主席树 )
把串倒过来插进trie上, 那么一个串的kpm串就是在以这个串最后一个为根的子树, 子树k大值的经典问题用dfs序+可持久化线段树就可以O(NlogN)解决 --------------------- ...
- BZOJ 3439 Kpm的MC密码 (Trie树+线段树合并)
题面 先把每个串反着插进$Trie$树 每个节点的子树内,可能有一些节点是某些字符串的开头 每个节点挂一棵权值线段树,记录这些节点对应的原来字符串的编号 查询的时候在线段树上二分即可 为了节省空间,使 ...
- BZOJ 3439: Kpm的MC密码 (trie+dfs序主席树)
题意 略 分析 把串倒过来插进trietrietrie上, 那么一个串的kpmkpmkpm串就是这个串在trietrietrie上对应的结点的子树下面的所有字符串. 那么像 BZOJ 3551/354 ...
- bzoj 3439 Kpm的MC密码(Trie+dfs序+主席树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3439 [题意] 给定若干串,问一个串的作为其后缀的给定串集合中的第k小. [思路] 如 ...
- BZOJ 3439 Kpm的MC密码
倒着建trie,然后主席树来求子树第k大. #include<iostream> #include<cstdio> #include<cstring> #inclu ...
- BZOJ 3531 [Sdoi2014]旅行 树链剖分+动态开点线段树
题意 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰. 为了方便,我们用 ...
- [bzoj 3531][SDOI2014]旅行(树链剖分+动态开点线段树)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3531 分析: 对于每个颜色(颜色<=10^5)都建立一颗线段树 什么!那么不是M ...
- BZOJ 3531: [Sdoi2014]旅行 (树剖+动态开点线段树)
对于每种信仰维护一棵动态开点线段树就行了- #include <cstdio> #include <cctype> #include <cstring> #incl ...
- [2016湖南长沙培训Day4][前鬼后鬼的守护 chen] (动态开点线段树+中位数 or 动规 or 贪心+堆优化)
题目大意 给定一个长度为n的正整数序列,令修改一个数的代价为修改前后两个数的绝对值之差,求用最小代价将序列转换为不减序列. 其中,n满足小于500000,序列中的正整数小于10^9 题解(引自mzx神 ...
随机推荐
- php输出缓冲区
ob_start(); echo 'aaa'; $string = ob_get_contents(); file_put_contents('a.html', $string); ob_flush( ...
- 【python】-- 函数非固定参数,返回值(return)
函数非固定参数 1.默认参数: 代码如下: def information_register(name,age,country,sex): print("----注册信息------&quo ...
- 修改maven的war包生成路径
因为要配合jenkins,所以控制了war包的生成目录: <plugins> <!--打war包到指定的目录下 --> <plugin> <groupId&g ...
- Latex: Expression under summation on multiple lines
Purpose Describe the sum symbol like this: zebk=1Nk∑i∈Rkj∈W(i)∩Rkmax(fi−fj) Solution code z_{ebk}=\f ...
- inux c编程:记录锁
记录锁的功能:当一个进程正在读或修改文件的某个部分是,它可以阻止其他进程修改同一文件区.对于这个功能阐述我认为有三点要解释的: 记录锁不仅仅可以用来同步不同进程对同一文件的操作,还可以通过对同一文件加 ...
- 图形数据库(GraphDB)
Date: 2016-01-27 Title: 图形数据库-1 Published: true Type: post Excerpt: Category: GraphDB 图形数据库(GraphDB) ...
- 数据分析R语言(1)
无意中发现网上的一个数据分析R应用教程,看了几集感觉还不错,本文做一个学习笔记(知识点来源:视频内容+R实战+自己的理解),视频详细的信息请参考http://www.itao521.com/cours ...
- ceph基本架构简述
1. 介绍 云硬盘是IaaS云平台的重要组成部分,云硬盘给虚拟机提供了持久的块存储设备.目前的AWS 的EBS(Elastic Block store)给Amazon的EC2实例提供了高可用高可靠的块 ...
- Python基础(4)_字典、集合、bool值
三.字典 定义:{key1:value1,key2:value2},key-value结构,key必须是不可变类型,或者可hash 基本形式:key:value特性: 1.可存放多个值 2.可修改指定 ...
- Thread.currentThread().getContextClassLoader() and Class.getClassLoader()
Thread.currentThread().getContextClassLoader() and Class.getClassLoader() 一.同一工程中: String path = T ...