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 }
  • 相关阅读:
    批处理 bat 查询局域网内在线电脑IP
    svn忽略不需要同步的文件夹或文件
    脚本设置IP bat 命令行设置自动获取IP和固定IP
    Lua中使用状态机FSM简单例子
    Lua 数组排序 table.sort的注意事项
    lua中怎么替换掉字符串中的$^特殊字符?
    C#应用程序隐藏调用bat脚本
    Oracle 基础教程
    Python编程
    利用Python进行数据分析(九)NumPy高级应用
  • 原文地址:https://www.cnblogs.com/QuentinYo/p/2994329.html
Copyright © 2011-2022 走看看