http://www.lydsy.com/JudgeOnline/problem.php?id=3998 (题目链接)

题意

  给出一个字符串,求它的字典序第K小的子串是什么,分情况讨论不在同一位置的相同子串需不需要重复考虑。

Solution

  对于不需要重复考虑的情况,直接就是spoj上的那道例题,而需要重复考虑的情况不过就是预处理sum的时候每次加上的是当前节点的right集合大小。

代码

// bzoj3998
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
#define LL long long
#define inf 1<<30
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=500010;
int n,T,K;
char s[maxn],ans[maxn]; namespace SAM {
int last,Dargen,sz,n;
int len[maxn<<1],ch[maxn<<1][26],par[maxn<<1];
int b[maxn],id[maxn<<1],sum[maxn<<1],r[maxn<<1];
void Extend(int c) {
int np=++sz,p=last;last=np;
len[np]=len[p]+1;
for (;p && !ch[p][c];p=par[p]) ch[p][c]=np;
if (!p) par[np]=Dargen;
else {
int q=ch[p][c];
if (len[q]==len[p]+1) par[np]=q;
else {
int nq=++sz;len[nq]=len[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
par[nq]=par[q];
par[np]=par[q]=nq;
for (;p && ch[p][c]==q;p=par[p]) ch[p][c]=nq;
}
}
}
void build() {
last=Dargen=sz=1;
n=strlen(s+1);
for (int i=1;i<=n;i++) Extend(s[i]-'a');
}
void pre() {
for (int i=1;i<=sz;i++) b[len[i]]++;
for (int i=1;i<=n;i++) b[i]+=b[i-1];
for (int i=1;i<=sz;i++) id[b[len[i]]--]=i;
if (!T) {
for (int i=1;i<=sz;i++) r[i]=1;
for (int S=0,i=sz;i>=1;i--,S=0) {
for (int j=0;j<26;j++) if (ch[id[i]][j]) S+=sum[ch[id[i]][j]];
sum[id[i]]=S+1;
}
}
else {
for (int p=Dargen,i=1;i<=n;i++) p=ch[p][s[i]-'a'],r[p]++;
for (int i=sz;i>=1;i--) r[par[id[i]]]+=r[id[i]];
for (int S=0,i=sz;i>=1;i--,S=0) {
for (int j=0;j<26;j++) if (ch[id[i]][j]) S+=sum[ch[id[i]][j]];
sum[id[i]]=S+r[id[i]];
}
}
}
void query() {
int tot=0,p=Dargen;
while (K>0) {
for (int i=0;i<26;i++) if (ch[p][i]) {
if (sum[ch[p][i]]>=K) {
p=ch[p][i];K-=r[p];
ans[++tot]=i+'a';
break;
}
else K-=sum[ch[p][i]];
}
}
ans[++tot]='\0';
}
}
using namespace SAM; int main() {
scanf("%s",s+1);
scanf("%d%d",&T,&K);
build();
pre();
query();
puts(ans+1);
return 0;
}

最新文章

  1. socket.io 中文手册 中文文档
  2. python PIL Image模块
  3. V8Sharp的中文乱码问题解决
  4. boot.img的分析
  5. UI进阶 多线程
  6. /Home/Tpl/Equipment/rangeIndex.html 里调用魔板
  7. 【转】C++之内部类(嵌套类)与外部类及友元
  8. 给你讲个笑话,我是创业公司CEO
  9. EL表达式 functions String处理函数
  10. mac 下有些工具 app 推荐
  11. 【Django】django 的request和response(转)
  12. 【JVM命令系列】jstack
  13. CNN网络架构演进:从LeNet到DenseNet
  14. 2016弱校联盟十一专场10.2 Longest Increasing Subsequence
  15. Linux入门:vi 和 vim
  16. codeforces580C
  17. VMware Ubuntu NAT 不能上网
  18. Java显示指定类型的文件
  19. 将一个List拆分为n份的方法
  20. Java工具:native2ascii ---得到中文对应的ASCII编码

热门文章

  1. Changing a remote&#39;s URL
  2. java.net.URLEncode编码 与 URLDecode解码问题
  3. HDOJ 1319 Prime Cuts&lt;数论&gt;
  4. drawRect &amp; layoutSubviews 调用时间
  5. FZU Problem 2028 时空门问题(DFS+优化)
  6. POJ 3258 River Hopscotch(二分查找答案)
  7. 关联容器(map):支持高效查找的容器,一种键值对的集合。
  8. Dojo和jQuery区别
  9. scrollView顶部空白
  10. 关于BOM 的详细介绍