二、stl ,模拟,贪心等 [Cloned] J - 归并排序求逆序对

原题:

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

You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length.

题意:

对几串字符串进行逆序数大小的排序,逆序数就是数学概念上的逆序数转化到字母上,例如序列中C在A的前边那么逆序数加一。给出的数据只是四种碱基ACGT,然后按照逆序数从小到大进行排序。

题解:

先定义一个结构体包括一个字符串和它的逆序数值,然后一个compare函数对结构体数组进行排序。重点在于怎么求一个字符串的逆序数,我借鉴了一篇博客的方法,定义一个四个位置的数组,然后对字符串从后到前进行搜索,如果是A,那么数组的三个位置都加1,因为之前无论出现剩下的哪三个字母逆序数都得加一,如果是C,那么对应G和T的数组加一,以此类推,在加数组的同时统计各个字母对应的逆序数。

代码:AC

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct
{
	char dna[120];
	int num;
}DNA;
DNA A[120];
bool compare(DNA a,DNA b)
{
	return a.num<b.num;
}
int count(char str[],int len)
{
	int a[4];
	memset(a,0,sizeof(a));
	int sum=0,i;
	for(i=len-1;i>=0;i--)
	{
		switch(str[i])
		{
			case'A':
			{
				a[1]++;
				a[2]++;
				a[3]++;
				break;
			}
			case'C':
			{
				a[2]++;
				a[3]++;
				sum+=a[1];
				break;
			}
			case'G':
			{
				a[3]++;
				sum+=a[2];
				break;
			}
			case'T':
			sum+=a[3];
		}
	}
	return sum;
}
int main()
{
	int n,m,i;
	cin>>n>>m;
	for(i=0;i<m;i++)
	{
		cin>>A[i].dna;
		A[i].num=count(A[i].dna,n);
	}
	sort(A,A+m,compare);
	for(i=0;i<m;i++)
		cout<<A[i].dna<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81395463
今日推荐