zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

  • 相关阅读:
    vue中 $event 的用法--获取当前父元素,子元素,兄弟元素
    vue的通信方式(二)---祖父孙三个级别的之间的隔代通信
    js获取当前页面url信息
    解决Vue刷新一瞬间出现样式未加载完或者出现Vue代码问题
    Java POI读取excel中数值精度损失
    Java读取Excel数值内容带.0或变科学计数法的解决办法
    Java专业术语
    jdbc连接数据库
    阿里云linux配置ftp服务
    mybatis批量更新表setting parameters 错误
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/7825607.html
Copyright © 2011-2022 走看看