poj1007DNA Sorting

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct DNA
{
	string str;
	int revpair;
};
bool cmp(const struct DNA &dna1,const struct DNA &dna2)
{
	return (dna1.revpair<=dna2.revpair);
}
int main()
{
	int m,n;//n is the length of strings,m is the number of strings.
	cin>>n>>m;
	struct DNA *dna=new struct DNA[m];
	for(int i=0;i<m;i++)
	{
		cin>>dna[i].str;dna[i].revpair=0;
		for(int x=0;x<n-1;x++)
			for(int y=x+1;y<n;y++)
				if(dna[i].str[x]>dna[i].str[y])
					dna[i].revpair++;
	}
	sort(dna,dna+m,cmp);
	for(int i=0;i<m;i++) cout<<dna[i].str<<endl;
	delete []dna;
	system("pause");
	return 0;
}

写的时候看了另一个博主对这道题的一些总结,感觉学到了不少。

1.写完不要不检查就submit,这样改起来也会心烦意乱;

2.AC之后可以看看别人AC的代码,学习别人的长处!

3.这是我第一次用C++的string类以及algorithm里的sort函数,感觉到了大大的方便啊;

4.像这种题,使用结构体把string和逆序数“封装”起来是个非常好的想法!

5.发现自己经常容易想太多,图难于其易。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值