ZOJ 1076 Gene Assembly

原题链接

题目大意:首先学习一个生物学的单词,exon:外显子,DNA序列中能够翻译表达的片段。给出很多外显子的起始点和终点,求寻找包含最多外显子的一条链,并且输出这些外显子的编号。

解法:先把所有外显子按照起始点的先后排序,相同起始位置的则看终止位置。然后就是一个一个查找,如果当前外显子的终点在下一个外显子之后,就跳过当前的;否则就输出当前外显子的编号。这里使用了vector来存储外显子。写了一段判断大小的代码,作为sort()函数的判断条件,有必要做个标记。

参考代码:

#include<iostream>
#include <algorithm>
#include <vector>
#include<string.h>
using namespace std;

struct Exon{
	int st,ed,index;
}t;

int lessthan(Exon a, Exon b){		//this function is necessary. Elements are compared based on this function.
	if(a.st<b.st) return 1;			//First sort by the start point of each exon.
	else if(a.st==b.st&&a.ed<b.ed) return 1;	//If start is equal, then sort by the end.
	else return 0;
}


int main(){
	int i,n;
	vector<Exon> exon;
	while(cin>>n&&n){
		int flag[1000]={0},j=0;
		memset(flag,0,sizeof(flag));
		while(!exon.empty()) exon.pop_back();		//remember to empty the vector!!!
		for(i=0;i<n;i++){
			cin>>t.st>>t.ed;
			t.index=i+1;
			exon.push_back(t);
		}
		sort(exon.begin(),exon.end(),lessthan);
		int tail=-1;
		for(i=0;i<n-1;i++){
			if(exon[i].st>=tail&&exon[i].ed<exon[i+1].ed){	
				flag[j++]=exon[i].index;
				tail=exon[i].ed;
			}
			else continue;
		}
		if(exon[n-1].st>=tail)flag[j]=exon[n-1].index;
		cout<<flag[0];
		for(i=1;i<=j;i++)
			cout<<' '<<flag[i];
		cout<<endl;
	}

	return 0;
}
原文地址:https://www.cnblogs.com/naive/p/3568762.html