POJ1007 DNA逆序数

DNA Sorting
Time Limit: 1000MS      Memory Limit: 10000K
Total Submissions: 108156       Accepted: 43326
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

思路:
线代里面学的逆序数,觉得有点意思的是,我开始node数组开55(题目说是0到50),怎么都够用了,但一提交,RE,查了好久bug,最后找不出来没办法只能把数组再开大点试试开到100,立马AC。。我ffffff佛慈悲
ps:确实可能存在这种问题,有可能它题目写的是0到50,就偏偏有可能给出了50多组数据,然后就爆了我的数组出现RE,所以以后建议别那么小气,稍微多开大一点数组。
代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Node{
    string str;
    int count;
}node[100];
bool compare(const Node &node1, const Node &node2)
{
    return (node1.count<node2.count);
}
int main()
{
    int m,n;
    cin>>m>>n;
    for(int i=0;i<n;i++)
    {
        cin>>node[i].str;
    }
    for(int i=0;i<55;i++)
    {
        if(!node[i].str.length()!=m) node[i].count=0;
        else node[i].count=100000;
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            for(int k=j+1;k<m;k++)
            {
                if(node[i].str[j]>node[i].str[k]) node[i].count++;
            }
        }
    }
    sort(node,node+n,compare);
    for(int i=0;i<n;i++)
    {
        cout<<node[i].str<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/csustudent007/article/details/80850956