目录

SP7258-SUBLEX - Lexicographical Substring Search

SP7258-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.

输入格式:

输出格式:

样例:

样例输入 1:

1
2
3
4
aaa
2
2
3

样例输出 1:

1
2
aa
aaa

思路:

实现:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <cstdio>
#include <cstring>

typedef long long LL;

const int MAXN = 90005;

template <typename _T>
void read(_T &x)
{
    x = 0;
    char s = getchar();
    int f = 1;
    while (s < '0' || '9' < s)
    {
        f = 1;
        if (s == '-')
            f = -1;
        s = getchar();
    }
    while ('0' <= s && s <= '9')
    {
        x = (x << 3) + (x << 1) + s - '0', s = getchar();
    }
    x *= f;
}

template <typename _T>
void write(_T x)
{
    if (x < 0)
    {
        putchar('-'), x = -x;
    }
    if (9 < x)
    {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

LL f[MAXN << 1];
int cnt[MAXN << 1], seq[MAXN << 1];
char A[MAXN], res[MAXN];
int ch[MAXN << 1][26], fa[MAXN << 1], mx[MAXN << 1];
int N, Q, K, rt, lst, tot, len;

void copy(const int a, const int b) { fa[a] = fa[b], mx[a] = mx[b], memcpy(ch[a], ch[b], sizeof ch[a]); }
void expand(const char c)
{
    int x = c - 'a', p = lst, cur = ++tot;
    mx[cur] = mx[p] + 1, lst = cur;
    while (p && !ch[p][x])
        ch[p][x] = cur, p = fa[p];
    if (!p)
    {
        fa[cur] = rt;
        return;
    }
    int q = ch[p][x];
    if (mx[q] == mx[p] + 1)
    {
        fa[cur] = q;
        return;
    }
    int nq = ++tot;
    copy(nq, q);
    mx[nq] = mx[p] + 1, fa[cur] = fa[q] = nq;
    while (p && ch[p][x] == q)
        ch[p][x] = nq, p = fa[p];
}

void topo()
{
    for (int i = 1; i <= tot; i++)
        cnt[mx[i]]++;
    for (int i = 1; i <= tot; i++)
        cnt[i] += cnt[i - 1];
    for (int i = tot; i; i--)
        seq[cnt[mx[i]]--] = i;
}

void DFS(const int u, int rnk)
{
    if (!rnk)
        return;
    int v;
    for (int i = 0; i < 26; i++)
    {
        if (!(v = ch[u][i]))
            continue;
        if (rnk <= f[v])
        {
            putchar('a' + i), DFS(v, rnk - 1);
            return;
        }
        else
            rnk -= f[v];
    }
}

int main()
{
    scanf("%s", A + 1);
    N = strlen(A + 1);
    rt = lst = ++tot;
    for (int i = 1; i <= N; i++)
        expand(A[i]);
    topo();
    for (int i = tot, u; i; i--)
    {
        u = seq[i];
        //一定要用拓扑序来dp!
        if (i > 1)
            f[u] = 1;
        for (int j = 0; j < 26; j++)
            if (ch[u][j])
                f[u] += f[ch[u][j]];
    }
    read(Q);
    while (Q--)
    {
        read(K);
        DFS(1, K), putchar('\n');
    }
    return 0;
}