Thinking of a simple algorithm problem <dna sorting> (about cin/template/new etc)

Source: Internet
Author: User

The first is yesterday at Peking University OJ online to see a simple algorithm topic, although simple, but how to complete a high-efficiency, concise, easy to understand the code for my this foundation is not good, just into the computer industry, small white is still meaningful. And in the process of writing code, will find their usual study will not find problems, so want to write down this blog, mainly to facilitate their understanding of the algorithm.

Come on, the question.

DNA sorting
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 91599 Accepted: 36781

Description

One measure of ' unsortedness ' in a sequence is the number of the pairs of entries, which is out of order with respect to each Other. For instance, with the letter sequence "Daabec", this measure is 5, since D was greater than four letters E is greater than one letter to it right. This measure was called the number of inversions in the sequence. The sequence ' AACEDGG ' have only one inversion (E and D)---it was nearly sorted---while the sequence "ZWQM" has 6 invers Ions (it is as unsorted as can---exactly the reverse of sorted).

You is 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 is of the same length.

Input

The first line contains the integers:a positive integer n (0 < n <=) giving the length of the strings; and a positive integer m (0 < M <=) giving the number of strings. These is 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 strings can be equally sorted and then output them according to the orginal order.

Sample Input

Ten 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCAT

Sample Output

Cccggggggaaacatgaagggatcagatttatcgatgcatttttggccaatttggccaaa

#01, this is a very simple topic, basically is to read the topic, the way of thinking out of the kind. This topic is mainly to solve the following minor problems.
0X001: The data structure for storing DNA sequences, storing this type of data, the best of course is a two-dimensional array. M,n is the user input in the process of running the program, obviously cannot be defined directly by Char n_dna[n][m].
Although the topic gives the M and n are less than or equal to 50, but set a too large array, there is a lot of wasted space, as a serious programmer, obviously is in the mind.
If so, we can define the array using the following method and allocate space.
1Template <typename t>2 //defining ARR data Types3 classARR4 {5  Public:6ARR (intlength)7     {8          This->length =length;9data =NewT[length];Ten     } OneS Mdata; AUnsignedintlength; - }; -typedef arr<Char>*P_of_arr;//p_of_arr is a pointer type of one-dimensional ARR array the voidMain () - { -     intm, N; -scanf"%d%d", &m, &n); +     //constructing an N_dna array -Arr<p_of_arr>N_dna (N); +//allocates space for each pointer to the data array of N_dna A      intT; at       for(t =0; T < N; t++) -      { -N_DNA.DATA[T] =Newarr<Char>(m); -      } -}

This is mainly used in two C + + knowledge, write out, to strengthen their understanding.

One is the use of TempTale, where the role of the keyword template allows class arr to be multi-directional, that is, class arr can have a variety of understanding, that is, class arr as a template, It can be reused in some other similar but different environments. In layman's words, it is the first act of pretending that T is a defined type. Let the compiler recognize it without error. When arr defines a variable, it then complements the type of T. Therefore, you can use the type defined by template templates in environments of different type T.

The second is the use of new, first we define an arr array arr<p_of_arr> N_dna (N); we can see the Arr constructor inside, this->length = Length;data = new T[length]; Pass N to length within the ARR domain and allocate an array of t[n] space and pass the pointer to data (note that here data is a double pointer, that is, the array is data[n], where DATA[0,1....N] is also a pointer, because when you define ARR, T is p_of _arr, and typedef arr<char>* P_OF_ARR;//P_OF_ARR is a pointer type of one-dimensional Arr array.

int t;       for 0; T < N; t++)     {         new arr<char>(M);     }

Then we loop, and then we give DATA[0,1...N] space. Each data[i] points to a one-dimensional type of arr data.

At this point, we have allocated an array of n_dna.data[n]->data[m].

#02 Data input, cin Understanding, CIN is the C + + library function inside a series of functions, Cin>>/cin. Get/cin. Getline ... The use of these functions is not mentioned here.

You can refer to the following two blog posts, which are relatively clear

The personal experience is to understand two aspects of these functions when using and understanding them.

First, whether the space,tab,enter is discarded from the buffer.

Second, Cin. Getline affects the subsequent input by how the input flag bit is affected when it is out of range.

Reference blog:

Http://www.cnblogs.com/A-Song/archive/2012/01/29/2331204.html

http://blog.csdn.net/dongtingzhizi/article/details/2299365

This is followed by a very general simple code, which defines an array of rel to record the inverse of the DNA, and then sequentially follows the inverse from small to large output DNA sequence.

The complete code is as follows:

1 #define_crt_secure_no_warnings2#include <iostream>3 using namespacestd;4 #defineMAX 127685Template <typename t>6 classARR7 {8  Public:9ARR (intlength)Ten     { One          This->length =length; Adata =NewT[length]; -     } -S Mdata; theUnsignedintlength; - }; -typedef arr<Char>*P_of_arr; - intMainvoid) + { -     intm, N; +scanf"%d%d", &m, &n); A     //constructing an N_dna array atArr<p_of_arr>N_dna (N); -      intT; -       for(t =0; T < N; t++) -      { -N_DNA.DATA[T] =Newarr<Char>(m); -      } in      inti,j,k; -      int*rel =New int[n]; to      //Input DNA sequence +Cin.getline (n_dna.data[0]->data, M +1); -       for(i =0; I < n; i++) theCin.getline (N_dna.data[i]->data, m+1); *      //cyclic traversal, using REL to record the inverse value of each DNA sequence $       for(k =0; K < n; k++)Panax Notoginseng      { -REL[K] =0; the           for(i =0; I < m; i++) +               for(j = i; J < M; J + +) A                  if(n_dna.data[k]->data[i]>n_dna.data[k]->Data[j]) therel[k]++; +      } -      int*usedrel =New int[n];//flag If rel is used $      //initialization is all 0 $       for(i =0; I < n; i++) -Usedrel[i] =0; -      //find the smallest inverse worthy DNA sequence, and output the       for(i =0; I < n; i++) -      {WuyiK =-1;//address that records the minimum inverse value the          intMin=max;//record the minimum inverse value -           for(j =0; J < N; J + +) Wu              if(Rel[j] < min&&usedrel[j]==0) -              { AboutMin =Rel[j]; $K =J; -              } -USEDREL[K] =1;//tag has been accessed -           for(j =0; J < M; J + +) Acout << n_dna.data[k]->Data[j]; +cout <<Endl; the      } -      return 0; $}

The results of the operation are as follows:

The input and output are not separated because the topic is required for input and output.

Thinking of a simple algorithm problem <dna sorting> (about cin/template/new etc)

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.