SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! Knowing that, his friend Kinan decided to test his skills so he gave him a string S and asked him Q questions of the form:
If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?
After knowing the huge number of questions Kinan will ask, Daniel figured out that he can't do this alone. Daniel, of course, knows your exceptional programming skills, so he asked you to write him a program which given S will answer Kinan's questions.
Example:
S = "aaa" (without quotes)
substrings of S are "a" , "a" , "a" , "aa" , "aa" , "aaa". The sorted list of substrings will be:
"a", "aa", "aaa".
Input
In the first line there is Kinan's string S (with length no more than 90000 characters). It contains only small letters of English alphabet. The second line contains a single integer Q (Q <= 500) , the number of questions Daniel will be asked. In the next Q lines a single integer K is given (0 < K < 2^31).
Output
Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.
Example
Input:
aaa
2
2
3 Output:
aa
aaa
Edited: Some input file contains garbage at the end. Do not process them.
题意:
给你一个串
Q个询问,在其去重子串中,字典序排名为K的字串是哪一个 ,并输出来
题解:
说一下后缀自动机的做法
设定F[i] 表示 以状态i为起点 所能 形成的不同字串的个数
求出来,再类似于贪心的找法求出答案串
后缀数组做更简单些
后缀自动机
#include <bits/stdc++.h>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std; const int N = 3e5+; const long long mod = ; int isPlus[N * ],endpos[N * ];int d[N * ];
int tot,slink[*N],trans[*N][],minlen[*N],maxlen[*N],pre;
int newstate(int _maxlen,int _minlen,int* _trans,int _slink){
maxlen[++tot]=_maxlen;minlen[tot]=_minlen;
slink[tot]=_slink;
if(_trans)for(int i=;i<;i++)trans[tot][i]=_trans[i],d[_trans[i]]+=;
return tot;
}
int add_char(char ch,int u){
int c=ch-'a',v=u;
int z=newstate(maxlen[u]+,-,NULL,);
isPlus[z] = ;
while(v&&!trans[v][c]){trans[v][c]=z;d[z]+=;v=slink[v];}
if(!v){ minlen[z]=;slink[z]=;return z;}
int x=trans[v][c];
if(maxlen[v]+==maxlen[x]){slink[z]=x;minlen[z]=maxlen[x]+;return z;}
int y=newstate(maxlen[v]+,-,trans[x],slink[x]);
slink[z]=slink[x]=y;minlen[x]=minlen[z]=maxlen[y]+;
while(v&&trans[v][c]==x){trans[v][c]=y;d[x]--,d[y]++;v=slink[v];}
minlen[y]=maxlen[slink[y]]+;
return z;
}
void init_sam() {
for(int i = ; i <= tot; ++i)
for(int j = ; j < ; ++j) trans[i][j] = ;
pre = tot = ;
}
long long f[N],all[N];
char a[N];
int cnt[N],pos[N];
void query(long long k) {
int p = ;
while(k) {
long long now = ;
for(int i = ; i < ; ++i) {
if(!trans[p][i]) continue;
if(f[trans[p][i]] >= k) {
k--;
p = trans[p][i];
printf("%c",i+'a');
break;
}
else k -= f[trans[p][i]];
}
}
printf("\n");
}
int main() {
scanf("%s",a);
int n = strlen(a);
init_sam();
for(int i = ; i < n; ++i)
pre = add_char(a[i],pre);
for(int i = ; i <= n; ++i) cnt[i] = ;
for(int i = ; i <= tot; ++i) cnt[maxlen[i]]++,all[i] = maxlen[i] - minlen[i] + ;
for(int i = ; i <= n; ++i) cnt[i] += cnt[i-];
for(int i = tot; i >= ; --i) pos[cnt[maxlen[i]]--] = i; //for(int i = 2; i <= tot; ++i) cout<<all[i]<<" "<<slink[i]<<endl; for(int i = tot; i >= ; --i) {
int v = pos[i];
f[v] = ;
for(int j = ; j < ; ++j) {
f[v] += f[trans[v][j]];
}
}
int Q;
scanf("%d",&Q);
while(Q--) {
long long k;
scanf("%lld",&k);
query(k);
}
return ;
}
后缀数组
#include <bits/stdc++.h>
inline long long read(){long long x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std; const int N = 3e5+; const long long mod = ; int *ran,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
bool cmp(int *r,int a,int b,int l) {
return r[a] == r[b] && r[a+l] == r[b+l];
}
void SA(int *r,int *sa,int n,int m) {
int *x=wa,*y=wb,*t;
for(int i=;i<m;++i)wm[i]=;
for(int i=;i<n;++i)wm[x[i]=r[i]]++;
for(int i=;i<m;++i)wm[i]+=wm[i-];
for(int i=n-;i>=;--i)sa[--wm[x[i]]]=i;
for(int i=,j=,p=;p<n;j=j*,m=p){
for(p=,i=n-j;i<n;++i)y[p++]=i;
for(i=;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=;i<m;++i)wm[i]=;
for(i=;i<n;++i)wm[x[y[i]]]++;
for(i=;i<m;++i)wm[i]+=wm[i-];
for(i=n-;i>=;--i)sa[--wm[x[y[i]]]]=y[i];
for(t=x,x=y,y=t,i=p=,x[sa[]]=;i<n;++i) {
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
}
ran=x;
}
void Height(int *r,int *sa,int n) {
for(int i=,j=,k=;i<n;height[ran[i++]]=k)
for(k?--k:,j=sa[ran[i]-];r[i+k] == r[j+k];++k);
} int n;
long long f[N];
char a[N];
void query(long long k) {
int pos = lower_bound(f+,f+n+,k) - f;
long long lll = k - f[pos-] + height[pos];
for(int i = sa[pos]; i < sa[pos]+lll; ++i)
printf("%c",a[i]);
printf("\n");
}
int main() {
scanf("%s",a);
n = strlen(a);
for(int i = ; i < n; ++i) r[i] = a[i] - 'a'+;
r[n] = ;
SA(r,sa,n+,);
Height(r,sa,n);
for(int i = ; i <= n; ++i)
f[i] = f[i-] + n - sa[i] - height[i]; int Q;
long long k;
scanf("%d",&Q);
while(Q--) {
scanf("%lld",&k);
query(k);
}
return ;
}
SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组的更多相关文章
- Spoj SUBLEX - Lexicographical Substring Search
Dicription Little Daniel loves to play with strings! He always finds different ways to have fun with ...
- SPOJ SUBLEX Lexicographical Substring Search - 后缀数组
题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...
- spoj SUBLEX (Lexicographical Substring Search) RE的欢迎来看看
SPOJ.com - Problem SUBLEX 这么裸的一个SAM,放在了死破OJ上面就是个坑. 注意用SAM做的时候输出要用一个数组存下来,然后再puts,不然一个一个字符输出会更慢. 还有一个 ...
- spoj SUBLEX - Lexicographical Substring Search【SAM】
先求出SAM,然后考虑定义,点u是一个right集合,代表了长为dis[son]+1~dis[u]的串,然后根据有向边转移是添加一个字符,所以可以根据这个预处理出si[u],表示串u后加字符能有几个本 ...
- spoj 7258 Lexicographical Substring Search (后缀自动机)
spoj 7258 Lexicographical Substring Search (后缀自动机) 题意:给出一个字符串,长度为90000.询问q次,每次回答一个k,求字典序第k小的子串. 解题思路 ...
- Lexicographical Substring Search (spoj7259) (sam(后缀自动机)+第k小子串)
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- ●SPOJ 7258 Lexicographical Substring Search
题链: http://www.spoj.com/problems/SUBLEX/题解: 后缀自动机. 首先,因为相同的子串都被存在了自动机的同一个状态里面,所以这就很自然的避免了重复子串的问题. 然后 ...
- SPOJ 7258 Lexicographical Substring Search(后缀自动机)
[题目链接] http://www.spoj.com/problems/SUBLEX/ [题目大意] 给出一个字符串,求其字典序排名第k的子串 [题解] 求出sam上每个节点被经过的次数,然后采用权值 ...
- SP7258 SUBLEX - Lexicographical Substring Search - 后缀自动机,dp
给定一个字符串,求本质不同排名第k小的子串 Solution 后缀自动机上每条路径对应一个本质不同的子串 按照 TRANS 图的拓扑序,DP 计算出每个点发出多少条路径 (注意区别 TRANS 图的拓 ...
随机推荐
- Codeforces Gym100971 G.Repair-思维题(切矩形板子) (IX Samara Regional Intercollegiate Programming Contest Russia, Samara, March 13)
这个题就是一块大板子,问你能不能切成两块要求的长宽的两块板子,一开始是按切板子想的,感觉有点麻烦. 直接反过来想,把两块要求的板子拼起来,填成一个大板子,看填出来的这个板子和题目给的板子比较,小于等于 ...
- https://www.cnblogs.com/netoxi/p/7258895.html
https://www.cnblogs.com/netoxi/p/7258895.html 应用服务和数据服务分离 需求/解决问题 随着网站业务的发展,越来越多的用户访问导致性能越来越差,越来越多的数 ...
- Oracle Forms Execute_Query Example To Fetch The Records From Database
Execute_Query command is used to fetch all the records for current database data block in Oracle For ...
- 2016summer 训练第一场
A.http://acm.hdu.edu.cn/showproblem.php?pid=5538 求表面积,只需要将所有的1*1的小块扫描一遍.将每一个块与他相邻四周进行比较,如果该快高度大,则将该快 ...
- vps
vps是指虚拟专用服务器(Virtual Private Servers),等同于一台远程计算机,有独立的IP地址,全天24小时不关机,可以部署博客.应用.服务 ***简称SS,可以用来搭建FQ服务器 ...
- Flutter开发记录part3
(1) 获取当前屏幕宽度 width: MediaQuery.of(context).size.width, (1) pull_to_refresh,smartrefresh 自定义文字: new S ...
- C#面试基础知识2
1.C#三层架构 C#三层架构急表示层(UI,User Interface),业务逻辑层(BLL BusinessLogicLayer),数据访问层(DAL Data Access Layer).三层 ...
- (原创)lightgbm 一些错误情况处理
1.做多分类问题时候(mutticlass),如果遇到 lightgbm.basic.LightGBMError: b'Number of classes should be specified an ...
- HDU 5402 Travelling Salesman Problem(多校9 模拟)
题目链接:pid=5402">http://acm.hdu.edu.cn/showproblem.php?pid=5402 题意:给出一个n×m的矩阵,位置(i.j)有一个非负权值. ...
- 【温故知新】——Bootstrap响应式知识点复习
前言:本文是自己在学习课程中的课程笔记,这里用来温故知新的,并非本人原创. 开发工具 1.记事本,Editplus,... ... 2.Sublime,Dreamweaver 3.Webstorm = ...