4072. 【TJOI2015】弦论(string)

博客围绕字符串展开,包含Description、input、output、Data Constraint和Code等方面内容,涉及信息技术领域中字符串相关的描述、输入输出要求、数据约束及代码实现等关键信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

在这里插入图片描述

input

在这里插入图片描述

output

在这里插入图片描述

Data Constraint

在这里插入图片描述

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read()
{
    char ch = getchar();
    int res = 0;
    while(!isdigit(ch))
        ch = getchar();
    while(isdigit(ch))
        res = (res << 3) + (res << 1) + (ch ^ 48),ch = getchar();
    return res;
}
const int N = 500005;
struct node
{
    int len,link,nxt[27];
};
node st[N << 1];
int tot,op,f[N << 1],t[N << 1],g[N << 1],k,siz[N << 1],last,ans;
char a[N];
inline void init()
{
    last = tot = 1;
}
inline void sa_extend(int c)
{
    int cur = ++tot,p = last;
    last = cur;
    st[cur].len = st[p].len + 1,siz[cur] = 1;
    for(;p&&!st[p].nxt[c];p = st[p].link) 
        st[p].nxt[c] = cur;
    if(!p)
        st[cur].link = 1;
    else
    {
        int q = st[p].nxt[c];
        if(st[p].len + 1 == st[q].len)
            st[cur].link = q;
        else 
        {
            int clo = ++tot;
            st[clo] = st[q];           
            st[clo].len = st[p].len+1;     
            for(;p&&st[p].nxt[c] == q;p = st[p].link)
                st[p].nxt[c] = clo;
            st[q].link = st[cur].link = clo;                 
        }
    }
}
int main()
{
    freopen("string.in","r",stdin);
    freopen("string.out","w",stdout);
    scanf("%s",a + 1),init();
    int len = strlen(a + 1);
    for(int i = 1;i <= len;i++)
        sa_extend(a[i]-'a');
    op = read(),k = read();
    for(int i = 1;i <= tot;i++)
        t[st[i].len]++;
    for(int i = 1;i <= tot;i++)
        t[i] += t[i - 1];
    for(int i = 1;i <= tot;i++)
        g[t[st[i].len]--] = i;
    if(op)
    {
        for(int i = tot;i;i--)
            siz[st[g[i]].link] += siz[g[i]];
    }
    else
        for(int i = tot;i;i--)
            siz[g[i]] = 1;
    siz[1] = 0;
    for(int i = tot;i;i--)
    {
        f[g[i]] = siz[g[i]];
        for(int j = 0;j < 26;j++)
        {
            if(st[g[i]].nxt[j])
                f[g[i]] += f[st[g[i]].nxt[j]];
        }
    }
    if(k > f[1])
    {
        cout << -1;
        return 0;
    }
    int now = 1;
    k -= siz[now];
    while(k)
    {
        int p = 0;
        while(k > f[st[now].nxt[p]])
            k -= f[st[now].nxt[p]],++p;
        now = st[now].nxt[p],putchar('a'+p),k -= siz[now];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值