poj 1007 DNA Sorting

Description

One measure of ``unsortedness'' in a sequence is thenumber 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 isgreater than four letters to its right and E is greater than one letter to itsright. This measure is called the number of inversions in the sequence. Thesequence ``AACEDGG'' has only one inversion (E and D)---it is nearlysorted---while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as canbe---exactly the reverse of sorted). 


You are responsible for cataloguing a sequence of DNA strings (sequencescontaining only the four letters A, C, G, and T). However, you want to catalogthem, 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 integern (0 < n <= 50) giving the length of the strings; and a positive integerm (0 < m <= 100) giving the number of strings. These are followed by mlines, each containing a string of length n.

Output

Output the list of input strings, arranged from ``mostsorted'' to ``least sorted''. Since two strings can be equally sorted, thenoutput 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

EastCentral North America 1998

 

1007 DNA 排序(感谢http://blog.sina.com.cn/s/blog_61eccf0e0100epql.html翻译)

题目大意:

     序列“未排序程度”的一个计算方式是元素乱序的元素对个数。例如:在单词序列“DAABEC'”中,因为D大于右边四个单词,E大于C,所以计算结果为5。这种计算方法称为序列的逆序数。序列“AACEDGG”逆序数为1(E与D)——近似排序,而序列``ZWQM'' 逆序数为6(它是已排序序列的反序)。

     你的任务是分类DNA字符串(只有ACGT四个字符)。但是你分类它们的方法不是字典序,而是逆序数,排序程度从好到差。所有字符串长度相同。

输入:

第一行包含两个数:一个正整数n(0<n<=50)表示字符串长度,一个正整数m(0<m<=100)表示字符串个数。接下来m行,每行一个长度为n的字符串。

输出:

输出输入字符串列表,按排序程度从好到差。如果逆序数相同,就原来顺序输出。

样例输入:

10 6

AACATGAAGG

TTTTGGCCAA

TTTGGCCAAA

GATCAGATTT

CCCGGGGGGA

ATCGATGCAT

 

样例输出:

CCCGGGGGGA

AACATGAAGG

GATCAGATTT

ATCGATGCAT

TTTTGGCCAA

TTTGGCCAAA

 

来源:

北美中东部1998

http://acm.pku.edu.cn/JudgeOnline/problem?id=1007

 先暴力求逆序数,虽然想找效率高的方法求,不过脑子笨,于是暴力了,然后写了个快排,好吧,我就是对系统提供的不放心(其实是我总忘记怎么用),然后第一次叫re了,原因是,m和n看反了......

#include <stdio.h>
#include <string.h>

int InversionNumber(char Dnastr[55]);
void qsort_my(int l, int h, char Dna[111][55], int invsN[111]);

int main(void)
{
	int m, n;
	int k = 0;
	char Dna[111][55];
	int invsN[111];

//	freopen("in.txt", "r", stdin);
	scanf("%d%d", &m, &n);

	for (int i = 0;i < n;i ++)
	{
		scanf("%s", Dna[k++]);
		invsN[k-1] = InversionNumber(Dna[k-1]);
	}
	qsort_my(0, n,  Dna, invsN);
    for (int i = 0;i < n;i ++)
	{
		printf("%s\n", Dna[i]);
	}
}

void qsort_my(int l, int h, char Dna[111][55], int invsN[111])
{
	if (h < l + 2) return;
	int e = h, p = l;
	while(l < h)
	{
		while(++l < e && invsN[l] <= invsN[p]);
		while(--h > p && invsN[h] >= invsN[p]);
		if (l < h)
		{
			int temp = invsN[l];
			invsN[l] = invsN[h];
			invsN[h] = temp;
			char temp_c[55];
			strcpy(temp_c, Dna[l]);
			strcpy(Dna[l], Dna[h]);
			strcpy(Dna[h], temp_c);
		}

	}
	int temp = invsN[h];
	invsN[h] = invsN[p];
	invsN[p] = temp;
	char temp_c[55];
	strcpy(temp_c, Dna[h]);
	strcpy(Dna[h], Dna[p]);
	strcpy(Dna[p], temp_c);

	qsort_my(p, h, Dna, invsN);qsort_my(l,e,Dna, invsN);
}
int InversionNumber(char Dnastr[55])
{
	int count = 0;
	for (int i = 0;i < strlen(Dnastr);i ++)
		for (int j = i+1;j < strlen(Dnastr); j++)
		{
			if (Dnastr[i] > Dnastr[j])
				count ++;
		}
	return count;
}


 是的,然后我尝试用归并排序写了一个计算逆序数个数的程序,虽然过了但是比上面暴力的速度慢....果然这道题的数据量太小,要不然就是我在哪里搞错了...>_<

#include <stdio.h>
#include <string.h>

#define MAXINT 65535
#define LENGTH 100

int InversionNumber(char Dnastr[55]);
void qsort_my(int l, int h, char Dna[111][55], int invsN[111]);

void Merge_sort(char a[LENGTH], int p, int r);
void Merge(char a[LENGTH], int p, int q, int r);

int inve_sum = 0;

int main(void)
{
	int m, n;
	int k = 0;
	char Dna[111][55];
	char Dna_cpy[55];
	int invsN[111];

    //freopen("in.txt", "r", stdin);
	scanf("%d%d", &m, &n);

	for (int i = 0;i < n;i ++)
	{
		scanf("%s", Dna[k++]);
		strcpy(Dna_cpy, Dna[k-1]);
		inve_sum = 0;
		Merge_sort(Dna_cpy, 0, m-1);
		invsN[k-1] = inve_sum;
		//invsN[k-1] = InversionNumber(Dna[k-1]);
	}
	qsort_my(0, n,  Dna, invsN);
    for (int i = 0;i < n;i ++)
	{
		printf("%s\n", Dna[i]);
	}
}

void qsort_my(int l, int h, char Dna[111][55], int invsN[111])
{
	if (h < l + 2) return;
	int e = h, p = l;
	while(l < h)
	{
		while(++l < e && invsN[l] <= invsN[p]);
		while(--h > p && invsN[h] >= invsN[p]);
		if (l < h)
		{
			int temp = invsN[l];
			invsN[l] = invsN[h];
			invsN[h] = temp;
			char temp_c[55];
			strcpy(temp_c, Dna[l]);
			strcpy(Dna[l], Dna[h]);
			strcpy(Dna[h], temp_c);
		}

	}
	int temp = invsN[h];
	invsN[h] = invsN[p];
	invsN[p] = temp;
	char temp_c[55];
	strcpy(temp_c, Dna[h]);
	strcpy(Dna[h], Dna[p]);
	strcpy(Dna[p], temp_c);

	qsort_my(p, h, Dna, invsN);qsort_my(l,e,Dna, invsN);
}

void Merge_sort(char a[LENGTH], int p, int r){
    int q;
    if (p < r){
        q = (p + r) >> 1;
        Merge_sort(a, p, q);
        Merge_sort(a, q + 1, r);
        Merge(a, p, q, r);
    }
}

void Merge(char a[LENGTH], int p, int q, int r){
    int L[(LENGTH >> 1) + 1], R[(LENGTH >> 1) + 1];
    int n1 = 0, n2 = 0;

    for (int i = p;i <= q;i ++){
        L[n1++] = a[i];
    }
    L[n1] = MAXINT;
    for (int i = q + 1;i <= r;i ++){
        R[n2++] = a[i];
    }
    R[n2] = MAXINT;

    n1 = 0, n2 = 0;
    for (int k = p;k <= r; k++){
        if (L[n1] <= R[n2]){
            a[k] = L[n1];
            n1++;
        }else{
            a[k] = R[n2];
            inve_sum += q - p + 1 - n1;
            n2++;
        }
    }
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值