ACM 每日水题以及小练习 2013年11月5日
2013-11-06 12:54
232 查看
POJ 1007
传送门:http://poj.org/problem?id=1007
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.
你的任务是分类DNA字符串(只有ACGT四个字符)。但是你分类它们的方法不是字典序,而是逆序数,排序程度从好到差。所有字符串长度相同。
即是输入m个长度为n的DNA序列,把他们按照逆序数从小到大稳定排序输出。
PS:“稳定排序”就是当序列中出现A1==A2时,排序前后A1与A2的相对位置不发生改变。
【未完待续】。。。。
传送门:http://poj.org/problem?id=1007
题目
DNA SortingDescription
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小练习 2013年11月6日(水题)
- Python每日练习 09 敏感词查找以及替换
- 每日ACM学习以及小水题 2013年11月3日
- 第十六周自由练习项目——acm 输入输出--保护继承
- ACM入门练习与递推小结
- 综合练习——哈希桶的实现以及迭代器
- acm练习2017 02 26
- 11月8日笔记练习以及心得。
- pyQt 每日一练习 -- 登录框
- YT04-贪心课后练习-1001—Saving HDU-(6.14日-烟台大学ACM预备队解题报告)
- 每日练习|Day016
- 每日练习2017-11-08
- ACM递推递归练习J 数学黑洞
- 算法练习——ACM_1001_Exponentiation
- 英语每日听写练习 Day 20
- sed的用法以及练习
- 动态规划练习二:HDU ACM 1159 Common Subsequence
- 菜鸟学习linux笔记与练习-----第二天。一些基本命令以及初级网络配置
- spark 窗口函数row_number练习以及用spark core实现
- vi 每日练习