UVa : 612 (DNA Sorting)



// http://uva.onlinejudge.org/external/6/612.html

Notes : 
use stable_sort in STL C++
i'm not quite sure abt the reason 
with the same code just using stable_sort i got acc 
before that i used bubble sort 
though it supposed to get acc as i preserve the input sequence when the measures are same 


// @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 <cmath>
#include <bitset>
#include <utility>
#include <set>
#define pi acos(-1.0)
#define N 1000000
using namespace std;

struct sequence {
    string ch;
    int measure;
} a [100 + 3];

int countMeasure (string p)
{
    int cnt = 0;

    for ( int i = 0; p [i]; i++ ) {
        for ( int j = i + 1; p [j]; j++ )
            if ( p [i] > p [j] ) cnt++;
    }

    return cnt;
}

bool cmp (sequence x, sequence y)
{
    if ( x.measure < y.measure ) return true;
    return false;
}

int main ()
{
    int testCase;
    scanf ("%d", &testCase);
    bool blank = false;

    while ( testCase-- ) {
        int n, m;
        scanf ("%d %d", &n, &m);

        for ( int i = 0; i < m; i++ ) {
            cin >> a [i].ch;
            a [i].measure = countMeasure (a [i].ch);
        }

        stable_sort (a, a + m, cmp);

        if ( blank ) printf ("\n");
        blank = true;

        for ( int i = 0; i < m; i++ )
            cout << a [i].ch << endl;
    }

	return 0;
}

// @END_OF_SOURCE_CODE
Advertisements

One thought on “UVa : 612 (DNA Sorting)

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