Suffix(hash+lcp+二分)

题目链接:

Suffix

Consider n given non-empty strings denoted by s1 , s2 , · · · , sn . Now for each of them, you need to select a corresponding suffix, denoted by suf1, suf2, · · · , sufn. For each string si, the suffix sufi is a non-empty substring whose right endpoint is the endpoint of the entire string. For instance, all suffixes of the string “jiangsu” are “u”, “su”, “gsu”, “ngsu”, “angsu”, “iangsu” and itself.

All selected suffixes could assemble into a long string T = suf_1suf1​​ + suf_2suf2​​ + · · · + suf_nsufn​​ . Here plus signs indicate additions of strings placing the latter at the tail of the former. Your selections of suffixes would determine the lexicographical order of T . Now, your mission is to find the one with minimum lexicographical order.

Here is a hint about lexicographical order. To compare strings of different lengths, the shorter string is usually padded at the end with enough “blanks” which is a special symbol that is treated as smaller than every letters.

Input

The first line of input contains an integer T which is the total number of test cases. For each case, the first line contains an positive integer n. Each of the following n lines contains a string entirely in lowercase, corresponding to s_1s1​​ , s_2s2​​ , · · · , s_nsn​​ . The summation of lengths of all strings in input is smaller or equal to 500000.

Output

For each test case, output the string T with minimum lexicographical order.

样例输入
3
3
bbb
aaa
ccc
3
aba
aab
bab
2
abababbaabbababba
abbabbabbbababbab
样例输出
baaac
aaabab
aab

题意:
n个字符串每个选择一个后缀依次连接,值得新得到的字符串字典序最小;

思路:
可以发现应该从后往前,把后面得到的字符串连接到第i个后面,再求这个的最小字典序的后缀,我写后缀数组T,所以采用hash+二分寻找和当前ans的lcp,然后比较lcp的下一位更新ans

AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long LL;
const int maxn=5e5+10;
const int x=123;
char s[maxn],tep[maxn],ans[maxn];
int le[maxn],anslen,p;
LL H[maxn],xp[maxn];
inline void init()
{
    xp[0]=1;
    for(int i=1;i<maxn;i++)xp[i]=xp[i-1]*x;
}
int check(int len)
{
    LL u=H[p]-H[p-len]*xp[len],v=H[anslen]-H[anslen-len]*xp[len];
    if(u!=v)return 0;
    return 1;
}
int main()
{
    init();
    int T;scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d ",&n);
        int sum=0;
        for(int i=1;i<=n;++i)
        {
            gets(s);
            le[i]=strlen(s);
            for(int j=0;j<le[i];j++)tep[sum+j]=s[j];
            sum=sum+le[i];
        }
        anslen=1,p=1;
        H[0]=0;ans[0]=0;
        for(int i=n;i>0;i--)
        {
            for(int j=0;j<le[i];j++,anslen++)
            {
                ans[anslen]=tep[--sum];
                H[anslen]=H[anslen-1]*x+(ans[anslen]-'a');
                if(j==0){p=anslen;continue;}
                int l=0,r=p;
                while(l<=r)
                {
                    int mid=(l+r)>>1;
                    if(check(mid))l=mid+1;
                    else r=mid-1;
                }
                if(l<p+1&&ans[anslen-l+1]<ans[p-l+1])p=anslen;
            }
            anslen=p+1;
        }
        for(int i=p;i>0;i--)printf("%c",ans[i]);puts("");
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/zhangchengc919/p/7825607.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值