UVa 10905 Children's Game 贪心

来源:互联网 发布:s7-300编程手册 编辑:程序博客网 时间:2024/05/16 15:04

/**
 * 这是贪心题吧? 或者就说排序吧。。
 * 只要保证string数组每个元素连接之后字典序最大就行了。。。
 * 其实类比下普通排序的比较数值大小就很好理解了,排序的时候,
 * 为了让最后数组从大到小或从小到大,使得每两个需要比较的元素都保持这个性质(在这里就是连接后字典序较大)
 * 这STL用的,还能这样啊。。长见识了。。我是看人代码的。。。
 *
 */

#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
int n;
string str[51];

int cmp(const string &a, const string &b) {
    return (a + b) > (b + a);
}

int main()
{
    int i;

    while(cin >> n, n) {
        for(i = 0; i < n; i ++)
            cin >> str[i];
        sort(str, str + n, cmp);
        for(i = 0; i < n; i ++)
            cout << str[i];
        cout << endl;
    }
    return 0;
}

原创粉丝点击