zoukankan      html  css  js  c++  java
  • ACM.DNA Sorting

    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 (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 "least sorted". If two or more strings are equally sorted, list them in the same order they are in the input.

    Sample Input

    10 6
    AACATGAAGG
    TTTTGGCCAA
    TTTGGCCAAA
    GATCAGATTT
    CCCGGGGGGA
    ATCGATGCAT

    Sample Output

    CCCGGGGGGA
    AACATGAAGG
    GATCAGATTT
    ATCGATGCAT
    TTTTGGCCAA
    TTTGGCCAAA

    分析:

    本题难点在于逆序数计算。惯性思维容易使人对字符串排序后进行比较,此方法复杂易错。

    简单的方法是数一数每个字符右边有几个比它大的字符。

    code:

     1 #include <iostream>
     2 #include <string>
     3 using namespace std;
     4 int len;
     5 int measureFunc(string s);
     6 int main()
     7 {
     8     int N;
     9     int measure[100];
    10     string ss[100];
    11     cin>>len>>N;
    12     cin.ignore();
    13     for(int i=0;i<N;i++)
    14     {
    15         cin>>ss[i];
    16     }
    17     for(int i=0;i<N;i++)
    18     {
    19         measure[i]=measureFunc(ss[i]);
    20     }
    21     int index=0;
    22     for(int i=0;i<N;i++)
    23     {
    24         index=0;
    25         for(int j=0;j<N;j++)
    26         {
    27             if(measure[index]>measure[j])
    28                 index=j;
    29         }
    30         cout<<ss[index]<<endl;
    31         measure[index]=65536;//置一个大数
    32     }
    33 
    34     return 0;
    35 }
    36 int measureFunc(string s)
    37 {
    38     int mea=0;
    39     for(int i=0;i<len;i++)   //计算字符串的逆序数
    40         for(int m=i+1;m<len;m++)
    41             if(s[i]>s[m])
    42                 mea++;
    43     return mea;
    44 }
  • 相关阅读:
    软件版本 —— Alpha、Beta、RC版本的区别
    linux删除回收站提示权限错误
    Centos7系统备份和还原脚本
    搭建Docker私服
    在深度Deepin 15.11系统上安装Python 3.7.4版本的方法
    Dockerfile范例
    Remmina无法远程连接的解决方法
    再生龙教程——对于Centos7进行备份和还原
    共享Excel编辑的一些资源
    【转载】怎样编写概要设计
  • 原文地址:https://www.cnblogs.com/QuentinYo/p/2994329.html
Copyright © 2011-2022 走看看