您的位置:首页 > 编程语言 > C语言/C++

ACM 每日水题以及小练习 2013年11月5日

2013-11-06 12:54 232 查看
POJ 1007

传送门:http://poj.org/problem?id=1007

题目

DNA Sorting

Description

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. 

Input

The first line contains two integers: a positive integer n (0 < n <= 50) giving the length of the strings; and a positive integer m (0 < m <= 100) giving the number of strings. These are followed by m lines, each containing a string of length n.
Output

Output the list of input strings, arranged from ``most sorted'' to ``least sorted''. Since two strings can be equally sorted, then output them according to the orginal order.

大致题意

 序列“未排序程度”的一个计算方式是元素乱序的元素对个数。例如:在单词序列“DAABEC'”中,因为D大于右边四个单词,E大于C,所以计算结果为5。这种计算方法称为序列的逆序数。序列“AACEDGG”逆序数为1(E与D)——近似排序,而序列``ZWQM'' 逆序数为6(它是已排序序列的反序)。

你的任务是分类DNA字符串(只有ACGT四个字符)。但是你分类它们的方法不是字典序,而是逆序数,排序程度从好到差。所有字符串长度相同。

即是输入m个长度为n的DNA序列,把他们按照逆序数从小到大稳定排序输出。

PS:“稳定排序”就是当序列中出现A1==A2时,排序前后A1与A2的相对位置不发生改变。

代码

/*****  简单ACM水题 ********/

/******** written by C_Shit_Hu ************/

////////////////POJ_1007///////////////

/****************************************************************************/
/*
没难度,先求各个字符串的逆序数,再按逆序数对字符串快排,用qsort()函数。
虽然快排不是稳定的排序,但是只要在定义排序规则函数cmp做适当处理,a==b时返回0,即不处理a和b,就不会改变他们之间的相对位置了。
*/
/****************************************************************************/

#include<iostream>
#include<algorithm>
using namespace std;

typedef class dna
{
public:
int num;  //逆序数
char sq[110];  //DNA序列
}DNAStr;

int InversionNumber(char* s,int len)
{
int ans=0;  //s逆序数
int A,C,G;  //各个字母出现次数,T是最大的,无需计算T出现次数
A=C=G=0;
for(int i=len-1;i>=0;i--)
{
switch(s[i])
{
case 'A':A++;break;  //A是最小的,无逆序数
case 'C':
{
C++;
ans+=A;  //当前C后面出现A的次数就是这个C的逆序数
break;
}
case 'G':
{
G++;
ans+=A;
ans+=C;
break;
}
case 'T':
{
ans+=A;
ans+=C;
ans+=G;
break;
}
}
}
return ans;
}

int cmp(const void* a,const void* b)
{
DNAStr* x=(DNAStr*)a;
DNAStr* y=(DNAStr*)b;
return (x->num)-(y->num);
}

int main(void)
{
int n,m;
while(cin>>n>>m)
{
DNAStr* DNA=new DNAStr[m];
for(int i=0;i<m;i++)
{
cin>>DNA[i].sq;
DNA[i].num = InversionNumber(DNA[i].sq,n);
}
qsort(DNA,m,sizeof(DNAStr),cmp);
for(int j=0;j<m;j++)
cout<<DNA[j].sq<<endl;
}
return 0;
}


【未完待续】。。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm algorithm CC++