ACM 每日水题以及小练习 2013年11月5日
来源:互联网 发布:有关小说的软件 编辑:程序博客网 时间:2024/05/21 19:36
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.
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.
大致题意
你的任务是分类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 每日水题以及小练习 2013年11月5日
- 每日ACM小练习 2013年11月6日(水题)
- 每日ACM学习以及小水题 2013年11月3日
- 每日ACM小题--POJ1006——中国剩余定理的完美诠释 2013年11月04(附录POJ_1005水题一枚)
- 11月8日笔记练习以及心得。
- 作业练习(11月5日)
- 2014年2月23日小练习的补题、
- 2014年3月2日的小练习补题。
- IDEA每日提醒2017年11月3日
- 10月11日练习
- 6月11日,每日20行。
- 5月28日每日股评
- 5月29日每日股评
- 5月7日,每日20行。
- 5月25日,每日20行。
- 5月26日,每日20行。
- 5月27日,每日20行。
- 5月28日,每日20行。
- sap启用应用
- 让你的旧电脑重获新生的5个Linux发行版
- jsp动态输出后台查询内容
- 【ios】论自定义的控件如何在xib中使用,自定义控件在xib中的初始化入口函数
- 【oracle】oracle 10g 登录em后,提示java.lang.Exception: Exception in sending Request :: null,解决办法
- ACM 每日水题以及小练习 2013年11月5日
- OPENGL|ES第四天,Adding color and shade
- 实现跨域cookie共享
- 设置边框效果和阴影层
- 欢迎到C + +教程
- c语言中指针的15个问题
- 搜索到的解决方法
- IOS判断文件类型
- STL算法概述