Suffix(hash+lcp+二分)

Posted LittlePointer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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_1suf?1?? + suf_2suf?2?? + · · · + suf_nsuf?n?? . 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_1s?1?? , s_2s?2?? , · · · , s_ns?n?? . 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;
}

  

以上是关于Suffix(hash+lcp+二分)的主要内容,如果未能解决你的问题,请参考以下文章

HUID 5558 Alice's Classified Message 后缀数组+单调栈+二分

BZOJ_1014_[JSOI2008]_火星人prefix_(Splay+LCP_Hash+二分)

BNU 34990 Justice String (hash+二分求LCP)

CF504E Misha and LCP on Tree 题解

bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)

后缀数组 hash求LCP BZOJ 4310: 跳蚤