SPOJ 7258 Lexicographical Substring Search(后缀自动机)
【题目链接】 http://www.spoj.com/problems/SUBLEX/
【题目大意】
给出一个字符串,求其字典序排名第k的子串
【题解】
  求出sam上每个节点被经过的次数,然后采用权值线段树上查找第k大数类似的方法,
      每次确定查找范围,进入相应的子树,同时输出路径上的点即可。
【代码】
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=500005;
char s[N];
struct sam{
int p,q,np,nq,cnt,last,a[N][26],l[N],f[N];
sam(){cnt=0;last=++cnt;}
void extend(int c){
p=last;np=last=++cnt;l[np]=l[p]+1;
while(!a[p][c]&&p)a[p][c]=np,p=f[p];
if(!p)f[np]=1;
else{
q=a[p][c];
if(l[p]+1==l[q])f[np]=q;
else{
nq=++cnt;l[nq]=l[p]+1;
memcpy(a[nq],a[q],sizeof(a[q]));
f[nq]=f[q]; f[np]=f[q]=nq;
while(a[p][c]==q)a[p][c]=nq,p=f[p];
}
}
}
int b[N],x[N],mx[N];
void build(){
scanf("%s",s+1);
int len=strlen(s+1);
for(int i=1;i<=len;i++)extend(s[i]-'a');
for(int i=1;i<=cnt;i++)b[l[i]]++;
for(int i=1;i<=len;i++)b[i]+=b[i-1];
for(int i=1;i<=cnt;i++)x[b[l[i]]--]=i;
for(int i=cnt;i;i--){
mx[x[i]]=1;
for(int j=0;j<26;j++)mx[x[i]]+=mx[a[x[i]][j]];
}
}
void query(int k){
p=1;
while(k){
for(int i=0;i<26;i++)if(a[p][i]){
if(mx[a[p][i]]>=k){
putchar('a'+i);
p=a[p][i];
--k;break;
}else k-=mx[a[p][i]];
}
}puts("");
}
}sam;
int main(){
sam.build();
int Q,x;
scanf("%d",&Q);
while(Q--){
scanf("%d",&x);
sam.query(x);
}return 0;
}
SPOJ 7258 Lexicographical Substring Search(后缀自动机)的更多相关文章
- spoj 7258 Lexicographical Substring Search (后缀自动机)
		
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
 - SPOJ 7258 Lexicographical Substring Search [后缀自动机 DP]
		
题意:给一个长度不超过90000的串S,每次询问它的所有不同子串中,字典序第K小的,询问不超过500个. 第一道自己做的1A的SAM啦啦啦 很简单,建SAM后跑kth就行了 也需要按val基数排序倒着 ...
 - SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
		
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
 - ●SPOJ 7258 Lexicographical Substring Search
		
题链: http://www.spoj.com/problems/SUBLEX/题解: 后缀自动机. 首先,因为相同的子串都被存在了自动机的同一个状态里面,所以这就很自然的避免了重复子串的问题. 然后 ...
 - SPOJ Lexicographical Substring Search 后缀自动机
		
给你一个字符串,然后询问它第k小的factor,坑的地方在于spoj实在是太慢了,要加各种常数优化,字符集如果不压缩一下必t.. #pragma warning(disable:4996) #incl ...
 - SPOJ SUBLEX Lexicographical Substring Search - 后缀数组
		
题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...
 - SPOJ 7258 Lexicographical Substring Search
		
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
 - SP7258 SUBLEX - Lexicographical Substring Search - 后缀自动机,dp
		
给定一个字符串,求本质不同排名第k小的子串 Solution 后缀自动机上每条路径对应一个本质不同的子串 按照 TRANS 图的拓扑序,DP 计算出每个点发出多少条路径 (注意区别 TRANS 图的拓 ...
 - SPOJ7258 SUBLEX - Lexicographical Substring Search(后缀自动机)
		
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
 
随机推荐
- js对JSON数据排序
			
一.适用于数字排序和字幕排序json 的排序方法有很多种,这是其中最简单的一种方法. 代码如下: var sortBy = function (filed, rev, primer) { rev ...
 - 编写一个程序实现strcpy函数的功能
			
#include <stdio.h> #include <string.h> #define N 5 char *mycpy(char *s1, char *s2) { //数 ...
 - Webpack 从0开始
			
Webpack Demos https://github.com/ruanyf/webpack-demos Docs https://webpack.github.io/docs/?utm_sourc ...
 - java后台开发- NOTE
			
2015-1-6: IDEA servlet-api.jar idea从14升级到15后,发现 import javax.servlet.AsyncContext; 找不到,右击工程,open mod ...
 - SQL Server 行的删除与修改-------------(未完待续P222 deep SQL Server 222 )
			
删除: 1.堆表:当行被删除时,不会自动重新组织页面上的空间.删除行时不会从物理页面上删除, 而只是把行偏移设置为 0 .表示空间没有使用.除了页面上没有被回收空间之外,堆中的 空白页也常常不会被回收 ...
 - Flex4开发笔记(与JAVA交互)
			
(由于本人也是第一次接触flex开发,因此将开发过程中问题记录留档) 一.数据交换过程 借助BlazeDS可以实现flex与java之间的数据交互,大体流程如下: 1.导入blazeds的文件(配置w ...
 - eclipse 快捷方式大全
			
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
 - 全排列的hash
			
我们经常使用的数的进制为“常数进制”,即始终逢p进1.例如,p进制数K可表示为K = a0*p^0 + a1*p^1 + a2*p^2 + ... + an*p^n (其中0 <= ai < ...
 - 字符串匹配——KMP算法
			
关于KMP算法的分析,我觉得这两篇博客写的不错: http://www.ruanyifeng.com/blog/2013/05/Knuth–Morris–Pratt_algorithm.html ht ...
 - 使用iscroll插件实现下拉刷新功能
			
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...