SPOJ SUBLEX: Lexicographical Substring Search 题解

Description

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.

Sample Input

aaa
2
2
3

Sample Output

aa
aaa

Edited

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


我们可以联想到字符串的所有子串都会对应到SAM的一个状态
首先对原串建SAM,然后考虑对每个点,计算从他出发还能走到多少个子串
这里我刚开始写sb了,要注意一个点的贡献不是maxi-mini,因为我已经固定了前面的路径,这部分会算多,联想把S的任意子串扔进SAM,一定会沿着转移边走到一个节点,所以每个点的贡献就是1,直接数size就好了
然后从SAM的根节点出发,每次按照边的字典序访问,如果当前sz小于k就换下一条边,否则沿着这条边走下去,把这条边对应的字符加到答案里
写的有点混乱,贴代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define LB long double
#define ull unsigned long long
#define x first
#define y second
#define pb push_back
#define pf push_front
#define mp make_pair
#define Pair pair<int,int>
#define pLL pair<LL,LL>
#define pii pair<double,double>

const int INF=2e9;
const LL LINf=2e16;
const int magic=348;
const int MOD=998244353;
const double eps=1e-10;
const double pi=acos(-1);

inline int getint()
{
    bool f;char ch;int res;
    while (!isdigit(ch=getchar()) && ch!='-') {}
    if (ch=='-') f=false,res=0; else f=true,res=ch-'0';
    while (isdigit(ch=getchar())) res=res*10+ch-'0';
    return f?res:-res;
}

const int MAXN=1e5;

char s[MAXN+48];int len;

namespace SAM
{
    int nxt[MAXN*2][30],par[MAXN*2],maxn[MAXN*2],minn[MAXN*2],tot;
    int root,last;
    inline void init() {root=last=++tot;}
    inline void insert(char ch)
    {
        int p=last,np=++tot,w=ch-'a'+1;maxn[np]=maxn[p]+1;
        while (p && !nxt[p][w]) nxt[p][w]=np,p=par[p];
        if (!p)
            par[np]=root;
        else
        {
            int q=nxt[p][w];
            if (maxn[q]==maxn[p]+1)
                par[np]=q;
            else
            {
                int nq=++tot;maxn[nq]=maxn[p]+1;
                memcpy(nxt[nq]+1,nxt[q]+1,26*sizeof(int));
                par[nq]=par[q];par[q]=par[np]=nq;
                while (p && nxt[p][w]==q) nxt[p][w]=nq,p=par[p];
            }
        }
        last=np;
    }
}

using namespace SAM;

int sz[MAXN*2],sm[MAXN*2];

inline int dfs(int cur)
{
    if (sz[cur]!=-1) return sz[cur];
    if (cur!=1) sz[cur]=1;
    for (register int i=1;i<=26;i++)
        if (nxt[cur][i]) sz[cur]+=dfs(nxt[cur][i]);
    return sz[cur];
}

string ans;
inline void getans(int cur,int k)
{
    int i;if (cur!=1) k--;
    if (!k) return;
    for (i=1;i<=26;i++)
        if (nxt[cur][i])
        {
            if (sz[nxt[cur][i]]>=k) {ans+=string(1,'a'+i-1);getans(nxt[cur][i],k);return;}
            k-=sz[nxt[cur][i]];
        }
}

int main ()
{
    int i;scanf("%s",s+1);len=strlen(s+1);
    init();for (i=1;i<=len;i++) insert(s[i]);
    for (i=2;i<=tot;i++) minn[i]=maxn[par[i]]+1;
    memset(sz,-1,sizeof(sz));
    dfs(1);int q=getint(),k;
    while (q--)
    {
        k=getint();ans="";
        getans(1,k);cout<<ans<<endl;
    }
    return 0;
}
  • 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、付费专栏及课程。

余额充值