SPOJ7258 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:ide


If all distinct substrings of string S were sorted lexicographically, which one will be the K-th smallest?this


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:spa


S = "aaa" (without quotes)
substrings of S are "a" , "a" , "a" , "aa" , "aa" , "aaa". The sorted list of substrings will be:
"a", "aa", "aaa".code

 

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).orm

Output

Output consists of Q lines, the i-th contains a string which is the answer to the i-th asked question.blog

Example

Input:
aaa
2
2
3

Output: aa
aaa

Edited: Some input file contains garbage at the end. Do not process them.ci

 

會作可是不會寫qwq,get

思路很簡單:input

把SAM的轉移邊造成的DAG圖求出來,而後跟主席樹查第$k$大同樣,貪心枚舉每一位,這個節點的某個兒子的大小大於$k$了,說明要找的串在這個兒子裏,string

不然就把$k$減去節點大小,繼續找

一開始弄不清楚DAG圖求出來的$size$和前綴樹求出來的$size$有啥區別。

大概就是:

對於每一個節點來講,DAG圖求出來的$size$表示有多少以該節點表明的字符爲起點的子串

前綴樹求出來的$size$表示該節點所表明狀態的$right$集合大小是多少,也就是該狀態出現了多少次

這題竟然卡dfs mmp

 

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN = 180001;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
char s[MAXN];
int opt, K, N;
int fa[MAXN], len[MAXN], ch[MAXN][27], siz[MAXN], tot = 1, last = 1, root = 1;
void insert(int x) {
    int now = ++tot, pre = last; last = now; len[now] = len[pre] + 1;
    siz[now] = 1;
    for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
    if(!pre) fa[now] = root;
    else {
        int q = ch[pre][x];
        if(len[q] == len[pre] + 1) fa[now] = q;
        else {
            int nows = ++tot; len[nows] = len[pre] + 1;
            memcpy(ch[nows], ch[q], sizeof(ch[q]));
            fa[nows] = fa[q]; fa[q] = fa[now] = nows;
            for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nows;
        }
    }
}    
void Query(int K) {
    int now = root;
    while(K) {
        if(now != root) K--;
        if(K <= 0) break;
        for(int i = 0; i <= 25; i++) 
            if(ch[now][i]) {
                if(siz[ch[now][i]] >= K) {putchar(i + 'a'); now = ch[now][i]; break; }
                else K -= siz[ch[now][i]];        
            }
    }
    puts("");
}
void Topsort() {
    static int A[MAXN], a[MAXN]; 
    for(int i = 1; i <= tot; i++) A[len[i]]++;
    for(int i = 1; i <= N; i++)   A[i] += A[i - 1];
    for(int i = tot; i >= 1; i--) a[A[len[i]]--] = i;
    for(int i = 1; i <= tot; i++) siz[i] = 1;
    for(int i = tot; i ; i--)
        for(int j = 0; j <= 25; j++)
            siz[a[i]] += siz[ch[a[i]][j]];    
}
int main() {
    scanf("%s", s + 1);
    N = strlen(s + 1);
    for(int i = 1; i <= N; i++) insert(s[i] - 'a');
    Topsort();
    int T = read();
    while(T--) {
        int K = read();
        Query(K);         
    }
    return 0; 
}
相關文章
相關標籤/搜索