Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

奉献代码!仅供参考!

Posted by suting at 2010-06-20 13:49:12 on Problem 1007
/* DNA排序
 * 根据DNA字符序列的“sortness”由高到低排序,同时需要稳定排序
 * 使用C++中的标准输入输出流在速度上要比printf,scanf慢
 * 这道题的本质还是域排序的问题,完全可以构造一个比较函数,还后使用推排序的算法
 * 启示:对于数组使用stable_sort()等STL算法后,数组本身的内容也会改变的
*/


#include<iostream>
#include<string>
#include<algorithm>
//#include<fstream>
#include<vector>
using namespace std;

#define MAX 102
int linenum,num;
int counter;

typedef struct{

	string seq;
	int unsortness;

}DNA;

DNA dna[MAX];

void process(string tmp){

	int unsortness=0;
	for(int i=0;i<linenum ; i++)
		for(int j=i+1;j< linenum ; j++){
			if( tmp[i]>tmp[j])
				unsortness++;
		}
	dna[counter].seq=tmp;
	dna[counter].unsortness=unsortness;
	counter++;
}

bool compare(DNA a,DNA b){				//返回false,表示不符合排序要求需要交换

	if( a.unsortness <= b.unsortness )
		return true;
	else
		return false;
}
int main(){

	//ifstream in("test.txt");
	//in>>linenum>>num;
	cin>>linenum>>num;

	string tmp;
	counter=0;
	for(int i=0; i<num; i++){

		//in>>tmp;
		cin>>tmp;
		process(tmp);

	}
	stable_sort(dna,dna+counter,compare);	//注意dna+counter表示end,不是dna+counter-1
	for(int j=0 ; j<counter ; j++)
		cout<<dna[j].seq<<endl;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator