POJ 1007
DNA Sorting
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 83069 Accepted: 33428 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. Sample Input 10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT Sample Output CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA
解法一:逆序数+快排(对cmp的完美诠释,原谅我刚刚学)+结构体(第一次在OJ用结构体)——
#include <iostream>
#include <algorithm>
#include <string>
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 = ; i < n; i++){
s[i].num = ;
cin>>s[i].w;
for(j = ; j < len; j++)
for(k = ; k < j; k++)
if(s[i].w[j] < s[i].w[k])//求逆序数
s[i].num++;
}
sort(s, s+n, cmp);
for(i = ; i < n; i++)
cout<<s[i].w<<endl;
return ;
}
解法二:这里进行求逆序数的有一种方法很巧妙,因为题目中只有4个字母,所以就用到了这种特殊性,我们可以得到O(n)求逆序数的方法;
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
char s[];//储存DNA序列
int sum;//储存每个DNA序列的逆序数
}a[];
bool cmp(node x,node y)//比较函数
{
return x.sum<y.sum;
}
int count_inver(char *str, int len)//求逆序数
{
int i;
int cnt = ;
int a[] = {};//用一个数组个保存字母出现的次数
for(i = len - ; i >= ; i--) {
switch (str[i]) {
case 'A':
a[]++;
a[]++;
a[]++;
break;
case 'C':
a[]++;
a[]++;
cnt += a[];
break;
case 'G':
a[]++;
cnt += a[];
break;
case 'T':
cnt += a[];
}
}
return cnt;
}
int main()
{
int m,n,i,j;
scanf("%d%d",&n,&m);
for(i=;i<m;i++)
{
scanf("%s",a[i].s);
a[i].sum=count_inver(a[i].s,n);
}
sort(a,a+m,cmp);
for(j=;j<m;j++)
printf("%s\n",a[j].s);
}
POJ 1007的更多相关文章
- poj 1007 DNA Sorting 解题报告
题目链接:http://poj.org/problem?id=1007 本题属于字符串排序问题.思路很简单,把每行的字符串和该行字符串统计出的字母逆序的总和看成一个结构体.最后把全部行按照这个总和从小 ...
- [POJ 1007] DNA Sorting C++解题
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77786 Accepted: 31201 ...
- Mathematics:DNA Sorting(POJ 1007)
DNA排序 题目大意:给定多个ACGT序列,按照字母顺序算出逆序数,按逆序数从小到大排列 这题其实很简单,我们只要用一个归并排序算逆序数,然后快排就可以了(插入排序也可以,数据量不大),但是要注意的是 ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
- poj 1007 (nyoj 160) DNA Sorting
点击打开链接 DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 75164 Accepted: 30 ...
- [POJ] #1007# DNA Sorting : 桶排序
一. 题目 DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 95052 Accepted: 382 ...
- poj 1007 DNA Sorting
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 95437 Accepted: 38399 Des ...
- poj 1007 纯水题 排序
#include<stdio.h> #include<string.h> #include<algorithm> #include<stdlib.h> ...
- POJ 1007 DNA Sorting(sort函数的使用)
Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are ...
随机推荐
- box-shadow IE8兼容处理
根据canisue(http://caniuse.com/#search=box-shadow),box-shadow兼容性如下图所示: 测试代码: <!DOCTYPE html> < ...
- iOS 如何保持线程一直在运转(二)
一.接着上一篇通过NSThread可以方便的创建一个线程,并且启动线程的Runloop,在线程体中执行一个while循环 然后我们就可以方便得利用这个线程了 - (void)threadRun:(NS ...
- vue学习笔记 模板语法(三)
<div id="kk"> <div>直接输出文本:{{msg}}</div> <div>自定义过滤器输出文本:{{msg|capi ...
- Tomcat源码分析(一)
这段时间简单的看了一下Tomcat的源码,在这里做个笔记! 1. tomcat 架构图 Catalina: tomcat的顶级容器,main()方法中就是通过,创建Catalina 对象实例,来启 ...
- plist文件的相关操作
本文概要 1.plist文件的简介 2.在Xcode中创建plist文件 3.在Xcode中将plist文件转换成数组或者字典对象 4.将数组或者字典对象转换成plist文件并且存储 详细介绍 1.p ...
- IOS设备型号(原创)
以下是我收集的ios目前为止移动设备型号,ipad air不知道,本人没有这款设备,求指导的给个回复,在这谢谢了 ///** //////////////////// 设备类型 字符串 /// ...
- axure 动态面板制作图片轮播 (01图片轮播)
利用Axure的动态面板组件制作图片轮播: 首先现在操作区添加一个动态面板组件: 鼠标放在动态面板上,右键单击选择面板状态管理,给动态面板设置名称并添加两条状态然后点击确定. 双击动态面板,然后双击s ...
- [Python]peewee使用经验
peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...
- Linux下deb包安装工具(附带安装搜狗输入法)
环境是在ubuntu14下的 #1.gdebi安装 使用deb安装工具gdebi,这个工具能解决所有依赖问题 sudo apt-get install gdebi #2.搜狗输入法 deb包下载地址: ...
- MTK elian(smartlink)在WIN32下的实现
先说明一下调试技巧:该程序需无线网卡实现功能,由于PC端有可能是多网卡的(有线网卡.无线网卡.虚拟网卡),所以在发包的时候数据包不一定会从无线网卡出,lib库应该也没处理多网卡的选择吧.所以在调试的时 ...