给定字符串,求其第K大子串
构造后缀自动机,按自动机中的step对其进行拓扑排序,一个字符串,若其后缀出现V次,那个它的出现次数则应加V,从后往前更新子串数量,然后从根节点开始查找第K大。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI 3.1415926
#define mod 1000000009
#define base 2333
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
const int maxx = 1e3 + 10;
inline void splay(int &v) {
v=0;char c=0;int p=1;
while(c<'0' || c >'9'){if(c=='-')p=-1;c=getchar();}
while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
v*=p;
}
struct Node {
Node *pre, *nxt[26];
int step, v;
void Clear() {
step = 0, v = 0;
pre = NULL;
memset(nxt, NULL, sizeof(nxt));
}
} *root, *last;
Node st[maxn<<1], *top[maxn<<1], *cur;
char str[maxn], ans[maxn];
int cnt[maxn];
void init() {
cur = st;
root = last = cur++;
root->Clear();
}
void extend(int w) {
Node *np = cur++, *p = last;
np->Clear();
np->step = p->step+1;
while(p && !p->nxt[w])
p->nxt[w] = np, p = p->pre;
if(p == NULL)
np->pre = root;
else {
Node *q = p->nxt[w];
if(q->step == p->step+1)
np->pre = q;
else {
Node *nq = cur++;
nq->Clear();
memcpy(nq->nxt, q->nxt, sizeof(q->nxt));
nq->step = p->step+1;
nq->pre = q->pre;
np->pre = q->pre = nq;
while(p && p->nxt[w] == q)
p->nxt[w] = nq, p = p->pre;
}
}
last = np;
}
void solve() {
scanf("%s", str);
init();
memset(cnt, 0, sizeof(cnt));
int len = strlen(str);
for(int i = 0; i < len; i++)
extend(str[i]-'a');
for(Node *p = st; p != cur; p++)
cnt[p->step]++;
for(int i = 1; i <= len; i++)
cnt[i] += cnt[i-1];
for(Node *p = st; p != cur; p++)
top[--cnt[p->step]] = p;
int num = cur-st;
for(int i = num-1; i >= 0; i--) {
top[i]->v = 1;
for(int j = 0; j < 26; j++) {
if(top[i]->nxt[j])
top[i]->v += top[i]->nxt[j]->v;
}
}
int q, x;
splay(q);
while(q--) {
splay(x);
Node *p = st;
int k = 0;
while(x) {
for(int i = 0; i < 26; i++) {
if(p->nxt[i]) {
if(p->nxt[i]->v >= x) {
ans[k++] = 'a'+i;
p = p->nxt[i];
x--;
break;
}
else
x -= p->nxt[i]->v;
}
}
}
ans[k] = '\0';
printf("%s\n", ans);
}
}
int main() {
//srand(time(NULL));
//freopen("kingdom.in","r",stdin);
//freopen("kingdom.out","w",stdout);
solve();
}