• 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 }
  • 相关阅读:
    Appium:四:控件
    Appium:三:APP元素定位
    jmeter分布式踩得坑汇总
    Linux环境下进行分布式压测踩过的坑
    记录一次余额迁移的坑(测试角度)
    记录性能测试脚本开发的过程
    jmeter如何设置全局变量
    性能测试,如何得到大量token,并保存在本地文件中
    小程序测试心得
    测试管理三
  • 原文地址:https://www.cnblogs.com/QuentinYo/p/2994329.html
Copyright © 2020-2023  润新知