Poj_1007: DNA sorting solution report

Source: Internet
Author: User

[General question] arrange multiple DNA sequences according to the "order degree" of each sequence ". If a sequence has been sorted in alphabetical order, the sequence has the highest degree of order, such as aaccggtt. On the contrary, if a sequence is more disordered, its ordering level is lower, such as tgtcaa.

[Solution] calculate the "Reverse Order Number" of each sequence, that is, the number of Reverse Order letter pairs. For example, the reverse order number of atgc is 3, because TG, TC, and GC are both backward. Then sort by the reverse order of each sequence. The larger the reverse order, the more unordered the sequence.

# Include <iostream >#include <algorithm> using namespace STD; const int sizen = 51; const int sizem = 101; struct s {char sequence [sizen]; int unsortedness ;} DNA [sizem]; int n, m; int CNT [4]; // Save the number of Enum {lettings for the current letter 'A' C' 'G' t, letterc, letterg, lettert}; bool CMP (s a, s B) {return. unsortedness <B. unsortedness;} int main () {int I, j; while (CIN> N> m) {cin. get (); for (I = 0; I <m; I ++) {cin. getline (DNA [I]. sequence, sizen) ;}for (I = 0; I <m; I ++) {memset (CNT, 0, sizeof (CNT); For (j = 0; j <n; j ++) {If (DNA [I]. sequence [J] = 'A') {CNT [letask] ++; DNA [I]. unsortedness + = CNT [letterc]; DNA [I]. unsortedness + = CNT [letterg]; DNA [I]. unsortedness + = CNT [lettert];} else if (DNA [I]. sequence [J] = 'C') {CNT [letterc] ++; DNA [I]. unsortedness + = CNT [letterg]; DNA [I]. unsortedness + = CNT [lettert];} else if (DNA [I]. sequence [J] = 'G') {CNT [letterg] ++; DNA [I]. unsortedness + = CNT [lettert];} else {CNT [lettert] ++ ;}} stable_sort (DNA, DNA + M, CMP); for (I = 0; I <m; I ++) {cout <DNA [I]. sequence <Endl ;}} return 0 ;}

[Code description] defines an array of struct to store multiple input DNA sequences, and each sequence corresponds to an integer value unsortedness to save its number in reverse order, and then sorts it in reverse order.

[Personal experience] It's just because I haven't been programming for too long, and I'm unfamiliar with it. It's stuck in two syntaxes.

The first is string reading. C and C ++ have a series of string reading methods, so it is hard to remember the differences between them, I have been entangled in the fact that the C-style code is still C ++-style, and I am reluctant to confuse the two, I was forced to use C ++ library functions as much as possible (which made me suffer a lot ).

Although this question took a long time, it was still rewarding. At least I knew how to read a line of strings (including spaces). Since we are used to using CIN and cout, we should keep them consistent, I use the "get" and "Getline" member functions of CIN to read a single character and a line of characters. Note that if you use CIN after CIN> N. getline (). Remember to use CIN first. get () read the Line Break From CIN> N in the input stream. Otherwise, Cin. getline () directly reads the line breaks left by CIN> N, and the string we want to read is not read.

The second is the call of the sort () function. At first, I used stable_sort (DNA [0], DNA [m], CMP); when I called it, an error occurred while compiling the result, I thought there was a problem with the structure or comparison function. After a long query, I almost changed the array to a vector, because the API uses the iterator as the parameter, it was changed to stable_sort (DNA, DNA + M, CMP.


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.