poj 1007 DNA Sorting 【水题】

本文介绍了一个具体的编程问题:如何统计字符串中每个字符后比该字符小的字符数量,并据此进行字符串排序。通过一个实例展示了使用冒泡排序解决该问题的过程,并提及了快速排序的尝试及其遇到的问题。

就是统计字符串每一个字符后面比这一字符小的数目;

链接 http://poj.org/problem?id=1007 

第一次是用冒泡做的 ac 因为题目中有这样一句话“Since two strings can be equally sorted, then output them according to the orginal order.

之后我又贼心不死,又用了快排试了一下,wa 果然。。。

代码:

#include <stdio.h>
#include <string.h>
typedef struct{
	char s[55];
	int sor;
}st;
int main()
{
	st str[105];
	int n, m, i, flag;
	scanf( "%d%d", &n, &m );
	for( i = 0; i < m; i ++ ){
		scanf( "%s", str[i].s );
		str[i].sor = 0;
		for( int j = 0; j < n-1; ++ j ){
			if( str[i].s[j] != 'A' )
			for( int k = j+1; k < n; ++ k ){
				if( str[i].s[j] > str[i].s[k] )
				++str[i].sor;
			}
		}
	}
	char temp[55];
	for( i = 0; i < m-1; i ++ )
	for( int j = 0; j < m-1-i; ++ j )
	if( str[j].sor > str[j+1].sor ){
		strcpy( temp, str[j].s );
		strcpy( str[j].s, str[j+1].s );
		strcpy( str[j+1].s, temp );
		int t = str[j].sor;
		str[j].sor = str[j+1].sor;
		str[j+1].sor = t;
	}
	for( i = 0; i < m; ++ i )
	printf( "%s\n", str[i].s );
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值