zoukankan      html  css  js  c++  java
  • 2017 青岛现场赛 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 = suf1suf_1suf1 + suf2suf_2suf2 + · · · + sufnsuf_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 s1s_1s1 , s2s_2s2 , · · · , sns_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个字符串,从每个字符串种选择一个最小后缀,要求把每个字符串的最小后缀拼接在一起组成一个字典序最小的字符串并输出

    题解:暴力,从最后一个字符串开始处理,用for找到每个字符串的最小后缀,再用s.substr()函数拼接后缀,输出结果

    注意:如果每次找到一个字符串的最小后缀,然后把所用后缀拼接起来可能并不是最终答案
    比如输入2个字符串
    aabaa
    cc
    第一个字符串的最小后缀是aa
    第二个最小后缀是c
    如果简单拼接,答案是aac
    但真正结果是aabaac
    因为后面的答案会影响前面的后缀串大小,因此我们得先求出最后的最小后缀串,然后将其加到上一个串的结尾,再求前面的串的最小的后缀串,以此类推再上一个串即可

    题目来源

    ACM-ICPC 2017 Asia Qingdao

     

    /*
    string s;
    s.substr(start,len);//字符串提取函数,从位置start开始提取长度为len的子串
    */
    
    #include<iostream>
    #include<stdio.h>
    #include<string>
    #include<set>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int  N=500005;
    string str[200005];
    int anslen,nowlen;
    int len[N];
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--)
        {
            str[0]="";
            int n;
            scanf("%d",&n);
            for(int i=1; i<=n; i++)
            {
                cin>>str[i];
                len[i]=str[i].size();
            }
     
            anslen=1;
            nowlen=1;
     
            for(int i=n; i>=1; i--)//从最后一个字符串开始处理
            {
                int pos=1;
                nowlen=str[i].size();
                for(int j=1; j<=len[i]; j++)//j<=len[i]保证处理每一个字符串的时候都会取一个后缀
                {
                    if(str[i].substr(pos-1,nowlen-pos+1)>str[i].substr(j-1,nowlen-j+1) )//找到后缀最小的字符串开始的位置pos
                    {
                        pos=j;
                    }
                }
                str[i-1]+=str[i].substr(pos-1,nowlen-pos+1);//将第i个字符串的最小后缀拼接到第i-1个字符串上
            }
            cout<<str[0]<<endl;
     
     
        }
     
        return 0;
    }
  • 相关阅读:
    django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
    Error fetching command 'collectstatic': You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path. Command 'collectstatic' skipped
    windows 虚拟环境下 安装 mysql 引擎一系列错误处理
    项目概念流程
    pip 使用
    HTTPserver v3.0 版本项目
    GitHub 使用
    git 操作命令详解
    git 忽略部分文件类型的同步
    Python 正则处理_re模块
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11387972.html
Copyright © 2011-2022 走看看