北大 ACM 1007 DNA Sorting

来源:互联网 发布:厦大吴春明 知乎 编辑:程序博客网 时间:2024/04/30 14:59
DNA Sorting
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 75079 Accepted: 30074

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 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT

Sample Output

CCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAATTTGGCCAAA
#include <iostream>#include <map>#include <string>#include <vector>#include <algorithm>//#define MAXFORVALUE 1000 // 所能容纳的最大权值 using namespace std;typedef pair<string, int>  PAIR; string *str=NULL;map<string, int> result;int count_num=0;int size = 0;string *repeat = new  string[50];int repeat_num = 0; void InputMessage();void SortForValue();void OutputMessage(); int main(){    InputMessage();    SortForValue();    OutputMessage();     //int f;    //cin >> f;    return 1;} int cmp(const PAIR& x,  const PAIR& y){    return x.second < y.second;} void OutputMessage(){    map<string, int>::const_iterator map_it = result.begin();    vector<PAIR> vecpair;     for(map<string, int>::iterator curr = result.begin(); curr != result.end(); ++curr)    {        vecpair.push_back(make_pair(curr->first, curr->second));    }    sort(vecpair.begin(), vecpair.end(), cmp);    int count_repeat = 0;    int j = 0;    int flag = false;    for(unsigned int i=0; i < vecpair.size(); i++)    {        for(int j = 0; j < repeat_num; j++)        {            if(vecpair[i].first == repeat[j])            {                cout << repeat[j] << endl;            }        }        cout << vecpair[i].first << endl;    }} void SortForValue(){    int value = 0; //得到字串的权值         for(int i=0; i<count_num; i++)    {        for(int j=0; j < size; j++)        {            for(int k=j+1; k <size; k++)            {                if(str[i][j] > str[i][k])                {                    value++;                }            }        }        pair<map<string, int>::iterator, bool> ret =             result.insert(make_pair(str[i], value));        if(!ret.second) //如果出现重复的字串        {            repeat[repeat_num] = str[i];            repeat_num++;        }        value = 0;    }} void InputMessage(){    cin >> size >> count_num;    str = new string[count_num];    for(int i=0; i < count_num; i++)        cin >> str[i];}








其实说实话,这道题目挺简单的,我觉得关键在于细节的处理方面。可是我却调试了2天......经过这一件事情,我觉得不管是在编写代码还是调试代码时都要心静,要同盘考虑




很享受在通过ACM 时,出现的那个“Accepted”!
原创粉丝点击