WA10 球调,玄关
查看原帖
WA10 球调,玄关
300098
cmaths楼主2024/1/8 20:40
#include <bits/stdc++.h>
#define ll long long

using namespace std;

int read()
{
    int x = 0, f = 1;
    char ch = 0;
    while(!isdigit(ch))
    {
        if(ch == '-')
        {
            f = -1;
        }
        ch = getchar();
    }
    while(isdigit(ch))
    {
        x = x * 10 + ch - '0';
        ch = getchar();
    }
    return x * f;
}

const int N = 500000, W = 26;
int len;
char str[N + 5];

struct Sam
{
    int len;
    int epsiz;
    ll f1, f2;
    int fa, nxt[W];
    Sam()
    {
        len = epsiz = f1 = f2 = 0;
        fa = 0;
        for(int t = 0; t < W; t++)
        {
            nxt[t] = 0;
        }
    }
}sam[N * 2 + 5];
struct Node
{
    int id, len;
    bool operator < (const Node &t) const
    {
        return len < t.len;
    }
}node[N * 2 + 5];
int n;
int last;
int newn()
{
    sam[++n] = Sam();
    return n;
}
void ist(int c)
{
    int cur = newn();
    sam[cur].len = sam[last].len + 1;
    node[cur] = {cur, sam[cur].len};
    sam[cur].epsiz = 1;
    int p;
    for(p = last; p && !sam[p].nxt[c]; p = sam[p].fa)
    {
        sam[p].nxt[c] = cur;
    }
    if(!p)
    {
        sam[cur].fa = 1;
    }
    else
    {
        int q = sam[p].nxt[c];
        if(sam[q].len == sam[p].len + 1)
        {
            sam[cur].fa = q;
        }
        else
        {
            int q1 = newn();
            sam[q1] = sam[q];
            sam[q1].len = sam[p].len + 1;
            sam[q1].epsiz = 0;
            node[q1] = {q1, sam[q1].len};
            for(; p && sam[p].nxt[c] == q; p = sam[p].fa)
            {
                sam[p].nxt[c] = q1;
            }
            sam[q].fa = sam[cur].fa = q1;
        }
    }
    last = cur;
}
bool vis[N + 5];
void dfs(int x)
{
    // printf("%d\n", x);
    if(vis[x])
    {
        // printf("114514");
        return;
    }
    vis[x] = 1;
    if(x != 1)
    {
        sam[x].f1 = 1;
        sam[x].f2 = sam[x].epsiz;        
    }
    for(int t = 0; t < W; t++)
    {
        if(sam[x].nxt[t])
        {
            dfs(sam[x].nxt[t]);
            sam[x].f1 += sam[sam[x].nxt[t]].f1;
            sam[x].f2 += sam[sam[x].nxt[t]].f2;
        }
    }
}

void ask1(int x, int k)
{
    if(x != 1)
    {
        k--;
    }
    if(k <= 0)
    {
        return;
    }
    for(int t = 0; t < W; t++)
    {
        if(sam[x].nxt[t])
        {
            if(k <= sam[sam[x].nxt[t]].f1)
            {
                printf("%c", t + 'a');
                ask1(sam[x].nxt[t], k);
                break;
            }
            k -= sam[sam[x].nxt[t]].f1;
        }
    }
}
void ask2(int x, ll k)
{
    if(x != 1)
    {
        k -= sam[x].epsiz;
    }
    if(k <= 0)
    {
        return;
    }
    for(int t = 0; t < W; t++)
    {
        if(sam[x].nxt[t])
        {
            if(k <= sam[sam[x].nxt[t]].f2)
            {
                printf("%c", t + 'a');
                ask2(sam[x].nxt[t], k);
                break;
            }
            k -= sam[sam[x].nxt[t]].f2;
        }
    }
}

void init()
{
    scanf(" %s", str + 1);
    len = strlen(str + 1);
    newn();
    node[1] = {1, 0};
    last = 1;
    for(int i = 1; i <= len; i++)
    {
        ist(str[i] - 'a');
    }
    sort(node + 1, node + n + 1);
    for(int i = n; i >= 1; i--)
    {
        int x = node[i].id;
        // printf("%d ", x);
        if(x == 1)
        {
            continue;
        }
        sam[sam[x].fa].epsiz += sam[x].epsiz;
    }
    dfs(1);
    // for(int i = 1; i <= n; i++)
    // {
    //     printf("%d ", sam[i].epsiz);
    // }    
    // printf("\n"); 
    // for(int i = 2; i <= n; i++)
    // {
    //     printf("%d %d\n", sam[i].fa, i);
    // }
    // for(int i = 1; i <= n; i++)
    // {
    //     for(int t = 0; t < W; t++)
    //     {
    //         if(sam[i].nxt[t])
    //         {
    //             printf("%d %d %c\n", i, sam[i].nxt[t], t + 'a');
    //         }
    //     }
    // }

    // for(int i = 1; i <= n; i++)
    // {
    //     printf("%d %d %d\n", i, sam[i].f1, sam[i].f2);
    // }
}

void solve()
{
    int t = read();
    ll k = (ll)read();
    if(t == 0)
    {
        if(sam[1].f1 < k)
        {
            printf("-1");
        }
        else
        {
            ask1(1, k);
        }
    }
    else
    {
        if(sam[1].f2 < k)
        {
            printf("-1");
        }
        else
        {
            ask2(1, k);
        }
    }
}

int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("gen.in", "r", stdin);
    freopen("my.out", "w", stdout);
#endif
    init();
    solve();
    return 0;
}
2024/1/8 20:40
加载中...