poj 1007 DNA Sorting (求逆序数)

DNA Sorting

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 83069   Accepted: 33428

Description

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 (0 < 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‘‘. Since two strings can be equally sorted, then output them according to the orginal order.

Sample Input

10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT

Sample Output

CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

Source

East Central North America 1998

一道简单的排序题,题目意思就是求逆序数,先求出每一段DNA序列的逆序数,然后对逆序数进行排序,然后就输出排好序之后的NDA序列;

这里进行求逆序数的有一种方法很巧妙,因为题目中只有4个字母,所以就用到了这种特殊性,我们可以得到O(n)求逆序数的方法,这就需要我们平时的多积累,和我们平时用归并排序或者用树状数组都要方便,对实际问题要进行具体分析;

下面是O(n)的求逆序数的代码;

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
    char s[100];//储存DNA序列
    int sum;//储存每个DNA序列的逆序数
}a[100];
bool cmp(node x,node y)//比较函数
{
    return x.sum<y.sum;
}
int count_inver(char *str, int len)//求逆序数
{
        int i;
        int cnt = 0;
        int a[4] = {0};//用一个数组个保存字母出现的次数
        for(i = len - 1; i >= 0; i--) {
                switch (str[i]) {
                        case 'A':
                                a[1]++;
                                a[2]++;
                                a[3]++;
                                break;
                        case 'C':
                                a[2]++;
                                a[3]++;
                                cnt += a[1];
                                break;
                        case 'G':
                                a[3]++;
                                cnt += a[2];
                                break;
                        case 'T':
                                cnt += a[3];
                }
        }
        return cnt;
}
int main()
{
    int m,n,i,j;
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
    {
        scanf("%s",a[i].s);
        a[i].sum=count_inver(a[i].s,n);
    }
    sort(a,a+m,cmp);
    for(j=0;j<m;j++)
        printf("%s\n",a[j].s);
}

由于这道题目的数据量不算太大,直接用朴素的求逆序数也能过;

逆序数就是看这个数前面有多少个数比当前的数大,这里直接用了一个二重循环;

#include <iostream>
#include <algorithm>
using namespace std;

struct f{
    int num;
    char w[50];
}s[101];
bool cmp(struct f a, struct f b){
    return a.num < b.num;
}
int main(){
    int i, len, n, j, k;
    cin>>len>>n;
    for(i = 0; i < n; i++){
        s[i].num = 0;
        cin>>s[i].w;
        for(j = 1; j < len; j++)
            for(k = 0; k < j; k++)
                if(s[i].w[j] < s[i].w[k])//求逆序数
                    s[i].num++;
    }
    sort(s, s+n, cmp);
    for(i = 0; i < n; i++)
        cout<<s[i].w<<endl;
    return 0;
}

时间: 2024-11-03 17:01:24

poj 1007 DNA Sorting (求逆序数)的相关文章

HDU 1379 DNA sorting(求逆序数)

DNA Sorting Problem Description 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 t

POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

POJ 2299 Ultra-QuickSort (归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 47235   Accepted: 17258 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 2299 Ultra-QuickSort 归并排序求逆序数对

题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题思路: 根据冒泡排序的特点,我们可知,本题只需要统计每一个数的逆序数(如果有i<j,存在a[i] > a[j],则称a[i]与 a[j]为逆序数对),输出所有的数的逆序数的和用普通排序一定会超时,但是比较快的排序,像快排又无法统计 交换次数,这里就很好地体现了归并排序的优点.典型的利用归并排序求逆

POJ 1007 DNA Sorting (归并排序)

题意: 输入m个长度为n的DNA序列,把他们按照逆序数从小到大稳定排序输出. PS:"稳定排序"就是当序列中出现A1==A2时,排序前后A1与A2的相对位置不发生改变. 思路:裸归并排序了  不懂得可以参考下http://blog.csdn.net/morewindows/article/details/6678165/  但是他的代码写错了!后来我按照理解自己写了一个,测了一下是对的! AC代码: #include<cstdio> #include<cstring&

POJ 1007 DNA Sorting

按逆序数从小到大排序.需要稳定排序.然而依然可以快排 #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int N = 55; const int M = 104; char str[M][N]; struct point{ int num, id; }p[M]; int n, m; void GetInverse(int id){ int i; in

POJ 2299 Ultra-QuickSort (求逆序数:离散化+树状数组或者归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 55048   Accepted: 20256 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 2299 Ultra-QuickSort 求逆序数,树状数组解法,详细解析

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 44554   Accepted: 16195 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include