博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1007
阅读量:5022 次
发布时间:2019-06-12

本文共 3517 字,大约阅读时间需要 11 分钟。

 
DNA SortingTime Limit: 1000MS           Memory Limit: 10000KTotal Submissions: 83069           Accepted: 33428DescriptionOne 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. InputThe 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.OutputOutput 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.Sample Input10 6AACATGAAGGTTTTGGCCAATTTGGCCAAAGATCAGATTTCCCGGGGGGAATCGATGCATSample OutputCCCGGGGGGAAACATGAAGGGATCAGATTTATCGATGCATTTTTGGCCAA TTTGGCCAAA

 

 

解法一:逆序数+快排(对cmp的完美诠释,原谅我刚刚学)+结构体(第一次在OJ用结构体)——

由于这道题目的数据量不算太大,直接用朴素的求逆序数绝对可以;
逆序数就是看这个数前面有多少个数比当前的数大,这里直接用了一个二重循环;
#include 
#include
#include
using namespace std; typedef struct f{ int num; string w; }data; bool cmp( data a, data b ){ return a.num < b.num; } int main(){ int i, len, n, j, k; cin>>len>>n; data *s = new data[n]; for(i = 0; i < n; i++){ s[i].num = 0; cin>>s[i].w; for(j = 1; j < len; j++) for(k = 0; k < j; k++) if(s[i].w[j] < s[i].w[k])//求逆序数 s[i].num++; } sort(s, s+n, cmp); for(i = 0; i < n; i++) cout<
<

解法二:这里进行求逆序数的有一种方法很巧妙,因为题目中只有4个字母,所以就用到了这种特殊性,我们可以得到O(n)求逆序数的方法;

#include 
#include
#include
using namespace std;struct node{ char s[100];//储存DNA序列 int sum;//储存每个DNA序列的逆序数}a[100];bool cmp(node x,node y)//比较函数{ return x.sum
= 0; i--) { switch (str[i]) { case 'A': a[1]++; a[2]++; a[3]++; break; case 'C': a[2]++; a[3]++; cnt += a[1]; break; case 'G': a[3]++; cnt += a[2]; break; case 'T': cnt += a[3]; } } return cnt;}int main(){ int m,n,i,j; scanf("%d%d",&n,&m); for(i=0;i

 

转载于:https://www.cnblogs.com/lipenglin/p/4381911.html

你可能感兴趣的文章
java的面向对象 (2013-09-30-163写的日志迁移
查看>>
HDU 2191 【多重背包】
查看>>
51nod 1433 0和5【数论/九余定理】
查看>>
【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】
查看>>
less 分页显示文件内容
查看>>
如何对数据按某列进行分层处理
查看>>
[Qt] this application failed to start because it could not find or load the Qt platform plugin
查看>>
Git Submodule管理项目子模块
查看>>
学会和同事相处的30原则
查看>>
NOJ——1568走走走走走啊走(超级入门DP)
查看>>
文件操作
查看>>
Python:GUI之tkinter学习笔记3事件绑定(转载自https://www.cnblogs.com/progor/p/8505599.html)...
查看>>
jquery基本选择器
查看>>
hdu 1010 dfs搜索
查看>>
搭建wamp环境,数据库基础知识
查看>>
android中DatePicker和TimePicker的使用
查看>>
SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现
查看>>
Android中获取应用程序(包)的大小-----PackageManager的使用(二)
查看>>
Codeforces Gym 100513M M. Variable Shadowing 暴力
查看>>
浅谈 Mybatis中的 ${ } 和 #{ }的区别
查看>>