1198. Substring

来源:互联网 发布:高性能mysql 编辑:程序博客网 时间:2024/05/29 03:07

1198. Substring

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Dr lee cuts a string S into N pieces,s[1],,s[N].   

Now, Dr lee gives you these N sub-strings: s[1],s[N]. There might be several possibilities that the string S could be. For example, if Dr. lee gives you three sub-strings {a,ab,ac}, the string S could be aabac,aacab,abaac,…   

Your task is to output the lexicographically smallest S. 

Input

        The first line of the input is a positive integer T. T is the number of the test cases followed.   

The first line of each test case is a positive integer N (1 <=N<= 8 ) which represents the number of sub-strings. After that, N lines followed. The i-th line is the i-th sub-string s[i]. Assume that the length of each sub-string is positive and less than 100. 

Output

The output of each test is the lexicographically smallest S. No redundant spaces are needed. 

Sample Input

1

3

a

ab

ac

Sample Output

aabac

Problem Source

ZSUACM Team Member

算法参考:http://blog.csdn.net/detective_xin/article/details/7215744

          http://blog.csdn.net/luojiayu14/article/details/7095364

两种代码:

C++理念:重点在sort函数的自定义函数上面。

// Problem#: 1198

// Submission#: 1952269

// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/

// All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include <iostream>

#include <string>

#include <cstring>

#include <algorithm>

using namespace std;

bool cmp(string a,string b){

    return a+b<b+a;

}

int main() {

    int n;

    cin>>n;

    int i;

    for(i=0;i<n;i++){

        int flag;

        cin>>flag;

        string *p = new string[flag];

        int counter;

        for(counter=0;counter<flag;counter++){

            cin>>p[counter];

        }

        sort(p,p+flag,cmp);

        for(counter=0;counter<flag;counter++){

            cout<<p[counter];

        }

        cout<<endl;

    }

    return 0;

}     

C理念:重点在于类似冒泡排序的东西上面。

// Problem#: 1198

// Submission#: 1952279

// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/

// All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include <iostream>

#include <string>

#include <cstring>

using namespace std;

int main() {

    int n;

    cin>>n;

    int i;

    for(i=0;i<n;i++){

        int flag;

        cin>>flag;

        char m1[250];

        char s1[250];

        char s2[250];

        char p[25][250];

        int counter;

        for(counter=0;counter<flag;counter++){

            cin>>p[counter];

        }

        int x,y,k;

        for(x=0;x<flag-1;x++){

            for(y=0;y<flag-1;y++){

        strcpy(s1,p[y]);

        strcpy(s2,p[y+1]);

        strcat(s1,p[y+1]);

        strcat(s2,p[y]);

        k=strcmp(s1,s2);

         if ( k>0 ) {

             strcpy(m1,p[y]);

             strcpy(p[y],p[y+1]);

             strcpy(p[y+1],m1);

         }

            }

        }

    for(counter=0;counter<flag;counter++){

        cout<<p[counter];

    }

        cout<<endl;

    }

    return 0;

}