Zoj problem set-1188 DNA sorting

Source: Internet
Author: User

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 ''ast 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
Aacatga.pdf
Ttttggccaa
Tttggccaaa
Gatcagattt
Cccgggggga
Atcgatgcat

Sample output
Cccgggggga
Aacatga.pdf
Gatcagattt
Atcgatgcat
Ttttggccaa
Tttggccaaa

Source:East central North America 1998

In this question, you only need to change the string sorting method and then use the STL container. However, in the question, if the comparison results are the same as those in the specified comparison method, the results will be output in the input order, which saves a lot of trouble.

The Code is as follows:

#include<iostream>
#include<set>
#include<string>
#include<algorithm>
#include<list>
using namespace std;
class DNA
{
public:
  string actg;
  DNA(string& s):actg(s){}
  bool operator<(const DNA& dna) const
  {
    return inversionNum() < dna.inversionNum();
  }
  int inversionNum() const
  {
    int bigToRight = 0;
    for(int i = 0; i < actg.length(); i++)
    {
      for(int j = i + 1; j < actg.length(); j++)
      {
        if(actg[i] > actg[j])
          bigToRight++;;
      }
    }
    return bigToRight;
  }
};
int main()
{
  int blocks;cin>>blocks;
  for(int block = 0; block < blocks; block++)
  {
    string state;
If (Block = 0) // There are only the following two empty rows at the first time
    {
Getline (CIN, State); // use Getline to eat the carriage return after the input Blocks
Getline (CIN, State); // empty rows required by the question
    }
    
    int n,m;
    cin>>n>>m;
    multiset<DNA> se;
    list<string> strList;
    for(int i = 0; i < m; i++)
    {
      string actg;cin>>actg;
      se.insert(DNA(actg));
    }
    for(multiset<DNA>::iterator it = se.begin(); it != se.end(); it++)
    {
      cout<<(*it).actg<<endl;
    }
If (block! = Blocks-1) // The last output has no blank lines
    {
      cout<<endl;
    }
  }
  return 0;
}

 

After that, I am thinking that if the comparison result is equal according to the comparison method in the meaning of the question, it will be sorted by the default comparison method of the string, <the reload method should be as follows:

bool operator<(const DNA& dna) const
{
  return inversionNum() < dna.inversionNum() ? true:actg<dna.actg;
}

However, if this is the case, the program will go wrong. I will never know where the error is because I have an error in the reload, what kind of behavior is caused by writing back like this? If I want to implement my ideas, how should I write the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.