POJ 1007 DNA Sorting

Description
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)---it is nearly sorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be---exactly the reverse of sorted).

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.

Input

The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.

Output

Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

Source

 
题目大意:其实题意很简单,但我还是花了好长时间才弄懂。。。按题目的例子来说:首先规定字母顺序从小到大(就是26字母的顺序),在``DAABEC'中D比右侧"AAB C"4个字母大,A在右侧没有找到比他小的,接下来的ABC类似,但是E比右侧的C大,所以``DAABEC''的”值”为4+1=5。现在应该明白为什么 ``AACEDGG''的值是1,``ZWQM''的值是6了吧。值相对小的称为 ``most sorted'',相对大的称为``least sorted''。现在给定几个字母序列,根据值的大小从小到大输出。
 
大致思路:水题。。。用结构体来记录字符串和它的值,比较输出即可。详见代码。
 
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
#define mem(a,b)  memset((a),(b),sizeof(a));
#define ll long long
const double eps = 1e-6;
const double pi = acos(-1.0);
#define INF 0x3f3f3f3f
#define maxn 105
#define mod 1000000007
using namespace std;
struct DNA
{
    string s;
    int num;
}a[maxn];
bool cmp(DNA x,DNA y)//要用sort比大小 所以要定义一个比较函数
{
    return x.num<y.num;
}
int main()
{
    //freopen("text.txt", "r", stdin);
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i=0;i<m;i++)
        {
            cin>>a[i].s;
            a[i].num=0;
            for(int j=0;j<n-1;j++)//遍历一遍字符串
            {
                for(int k=j+1;k<n;k++)
                {
                    if(a[i].s[j]>a[i].s[k])
                        a[i].num++;
                }
            }
        }
        sort(a,a+m,cmp);
        for(int i=0;i<m;i++)
            cout<<a[i].s<<endl;
    }
    return 0;
}

 

posted on 2017-09-03 11:10  FTA_Macro  阅读(122)  评论(0编辑  收藏  举报

导航