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

后缀自动机解法见【这里】
建出来sa,把询问排序之后在sa上扫描一遍,每个后缀会贡献 nsa[i]+1height[i] 个本质不同的子串。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct query
{
    int id,k;
    bool operator < (const query &qq) const
    {
        return k<qq.k;
    }
}a[510];
char s[200010];
int sa[200010],rank[200010],hei[200010],cnt[200010],f[200010],ansp[510],anslen[510],
n,m=26,q;
int main()
{
    int p,now=0;
    scanf("%s",s+1);
    n=strlen(s+1);
    for (int i=1;i<=n;i++) cnt[rank[i]=s[i]-'a'+1]++;
    for (int i=2;i<=m;i++) cnt[i]+=cnt[i-1];
    for (int i=n;i;i--) sa[cnt[rank[i]]--]=i;
    for (int k=1;k<=n;k<<=1)
    {
        p=0;
        for (int i=n-k+1;i<=n;i++) f[++p]=i;
        for (int i=1;i<=n;i++) if (sa[i]>k) f[++p]=sa[i]-k;
        for (int i=1;i<=m;i++) cnt[i]=0;
        for (int i=1;i<=n;i++) cnt[rank[f[i]]]++;
        for (int i=2;i<=m;i++) cnt[i]+=cnt[i-1];
        for (int i=n;i;i--) sa[cnt[rank[f[i]]]--]=f[i];
        for (int i=1;i<=n;i++) f[i]=rank[i];
        rank[sa[1]]=1;
        for (int i=2;i<=n;i++)
            rank[sa[i]]=rank[sa[i-1]]+
                (f[sa[i]]!=f[sa[i-1]]||f[sa[i]+k]!=f[sa[i-1]+k]);
        if (rank[sa[n]]>=n) break;
        m=rank[sa[n]];
    }
    for (int i=1;i<=n;i++)
    {
        hei[rank[i]]=hei[rank[i-1]];
        if (hei[rank[i]]) hei[rank[i]]--;
        while (s[i+hei[rank[i]]]==s[sa[rank[i]-1]+hei[rank[i]]]) hei[rank[i]]++;
    }
    scanf("%d",&q);
    for (int i=1;i<=q;i++)
    {
        scanf("%d",&a[i].k);
        a[i].id=i;
    }
    sort(a+1,a+q+1);
    p=1;
    for (int i=1;i<=q;i++)
    {
        while (now+n-sa[p]+1-hei[p]<a[i].k)
        {
            now+=n-sa[p]+1-hei[p];
            p++;
        }
        ansp[a[i].id]=sa[p];
        anslen[a[i].id]=hei[p]+a[i].k-now;
    }
    for (int i=1;i<=q;i++)
    {
        for (int j=ansp[i];j<=ansp[i]+anslen[i]-1;j++) putchar(s[j]);
        putchar('\n');
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值