2017 Qingdao-site game 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.

Sample input

3 in a 
3 in a 
of the BBB 
, with AAA 
But while the CCC 
3 in a 
the ABA 
AAB 
Bab 
2 for 
abababbaabbababba 
abbabbabbbababbab

Sample Output

baaac 
aaabab 
AAB 

question is intended : to n strings, select a minimum suffix string from each species, the minimum required for each suffix string is spliced together to form a string of lexicographically smallest output and

problem solutions : violence, from last string processing begins, find the minimum for extension with each string, then s.substr () function splicing suffix, the output

NOTE : If every time a suffix string to find the minimum, and then stitching together the suffixes may not be the final answer to
such input two strings
aabaa
CC
first string aa minimum suffix is
the second smallest suffix is c
if a simple stitching, the answer is aac
but the real result is aabaac
because the answers later will affect the front postfix size, we must first determine the minimum final suffix string is then applied to the end of a string, and then seek the smallest suffix string preceding string, and so on and then on to a string

Source title

ACM-ICPC 2017 Asia Qingdao

 

/ * 
String S; 
s.substr (start, len); // string extraction function, extracted length from the start position to start the len substring 
* / 

#include <the iostream> 
#include <stdio.h> 
#include < String > 
#include < SET > 
#include <algorithm>
 the using  namespace STD; 
typedef Long  Long LL;
 const  int   N = 500 005 ;
 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( J;int I = n-; I> = . 1 ; i--) // from the last string processing begins 
        {
             int POS = . 1 ; 
            nowlen = STR [I] .size ();
             for ( int J = . 1 ; J <= len [I]; J ++) // J <= len [I] to ensure that when each of the processing will take a suffix string 
            {
                 IF (STR [I] .substr (POS- . 1 , nowlen-+ POS . 1 )> STR [ I] .substr (J- . 1 , J +-nowlen . 1 )) // find the start position pos suffix string smallest 
                { 
                    pos = 
                } 
            } 
            STR [i - . 1 ] + = STR [i] .substr (POS- . 1 , nowlen-+ POS . 1 ); // splicing the i-th smallest suffix string to the first string of i-1 
        } 
        COUT STR << [ 0 ] << endl; 
 
 
    } 
 
    return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/-citywall123/p/11387972.html