ZOJ Problem Set - 1188 DNA Sorting

来源:互联网 发布:淘宝折扣返利网 编辑:程序博客网 时间:2024/06/09 06:04
DNA Sorting

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


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 (1 < 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''. If two or more strings are equally sorted, list them in the same order they are in the input file.


Sample Input

1

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT


Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA


Source: East Central North America 1998




分析:

字符串的排序问题。

题意:

给若干字符串。对于某一字符串,固定某个字符,向右扫描,若它的右边有n个字符比它大,则sum+=n。把字符串的所有字符都照此规则扫描一遍,得到的sum就是字符串的“倒位数量”。对于给定的若干字符串,要求按照sum的大小从小到大输出。

输入有多个块,每个块之间输出一个换行。



ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> v;
string s;
bool cmp(const string &s1,const string &s2)//重点是排序函数,熟悉写法
{
    int i,j,k,c1=0,c2=0;
    int L1=s1.length(),L2=s2.length();
    for(i=0;i<L1;i++)
    for(j=i+1;j<L1;j++)
    {
        if(s1[i]>s1[j])
        c1++;
    }
    for(i=0;i<L2;i++)
    for(j=i+1;j<L2;j++)
    {
        if(s2[i]>s2[j])
        c2++;
    }
    return c1<c2;
}
int main()
{
    int t,n,m,i,j;
    scanf("%d",&t);
    int c=0;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        v.clear();//注意清空vector容器
        for(i=0;i<m;i++)
        {
            cin>>s;
            v.push_back(s);
        }
        if(c) printf("\n");
        sort(v.begin(),v.end(),cmp);
        for(i=0;i<m;i++)
        cout<<v[i]<<endl;
        c++;
    }
    return 0;
}

0 0
原创粉丝点击