UVa : 10905 (Children’s Game)


// http://uva.onlinejudge.org/external/109/10905.html

// @BEGIN_OF_SOURCE_CODE

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <bitset>
#include <utility>
#include <set>
#include <math.h>
#define pi acos(-1.0)
#define N 1000000
using namespace std;

bool cmp (string x, string y)
{
    string t1 = x + y;
	string t2 = y + x;

	if ( t1 > t2 ) return true;
	return false;
}
int main ()
{
    int n;
    string a [50 + 5];

    while ( scanf ("%d", &n) && n ) {

        for ( int i = 0; i < n; i++ )
            cin >> a [i];

        sort (a, a + n, cmp);

        for ( int i = 0; i < n; i++ )
            cout << a [i];

        printf ("\n");
    }

    return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

5 thoughts on “UVa : 10905 (Children’s Game)

  1. @Drisho
    string a [50 + 5];

    is same as,

    string a [55];

    this style of coding means, you need 50 elements and you took 5 for safety / extra

  2. I cannot understand about the work of “cmp” function following by ” sort (a, a + n, cmp);”, can you please help me… 😦

  3. @choice
    cmp function defines how the sort should work

    what do we do in sorting? we decide whether a string should come first than another, right?

    Here, in cmp function we have two strings: x and y. In the body of cmp function we are deciding which one should come first

    usually the comparison is like this, (in case of ascending sort)


    if ( x < y ) then x should come first
    otherwise y should come first

    for the purpose of this particular problem and algorithm, we are doing this like,


    if (x + y > y + x) then x should come first
    otherwise y should come first

    Hope it helps 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s