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 ...
随机推荐
- java学习笔记 --- 数组
一.Java的内存分配 A:栈内存: 存储局部变量,只要是在方法中定义的变量都是局部变量.一旦变量的生命周期结束该变量就被释放. B:堆内存: 存储所有new出来的,及实体(对象),每一个实体 ...
- 小程序新能力-个人开发者尝鲜微信小程序
个人开发者的福利 微信小程序,刚听到这个新名词的时候,我就兴冲冲的去找入口,看看自己能不能搞个微信小程序的HelloWorld,毕竟能在微信上把自己写的一些小工具跑起来还是满炫酷的. 没想,网上一查, ...
- 使用Python对Excel进行读写操作
学习Python的过程中,我们会遇到Excel的读写问题.这时,我们可以使用xlwt模块将数据写入Excel表格中,使用xlrd模块从Excel中读取数据.下面我们介绍如何实现使用Python对Exc ...
- 【C语言】模拟实现atoi函数
atoi(表示 ascii to integer)是把字符串转换成整型数的一个函数. atoi()函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( ...
- 【Egret】里使用video标签
egret里使用Html5的Video标签 egret里使用Html5的Video标签的demo: 链接:http://pan.baidu.com/s/1nuNyqRR 密码:x58i //----- ...
- 现代3D图形编程学习-你好,三角形(译)
你好,三角形 传统的入门教程在介绍编程语言的时候,通常从"Hello,World!"的程序开始.这样的程序拥有最简单的能够直接输出"Hello, World!" ...
- Appium和Robotium在文字输入上的区别
Appium和Robotium在文字输入上的区别 Appium和Robotium在对文本框进行输入时有一定的区别: Appium在输入文字时需要调用系统键盘 Robotium在输入文字是根本不需要 ...
- 测试开发Python培训:模拟登录新浪微博-技术篇
测试开发Python培训:模拟登录新浪微博-技术篇 一般一个初学者项目的起点就是登陆功能的自动化,而面临的项目不同实现的技术难度是不一样的,poptest在做测试开发培训中更加关注技术难点,掌握技 ...
- DataTables源码分析(一)
DataTables源码分析 写在前面 作为一名常年奋战在java世界中的程序猿,当我接触到现在所谓的前端技术时,内心其实是崩溃的.因为,前端的技术给我的第一个感觉就是"乱",这里 ...
- 08 Noise and Error
噪声:误标.对同一数据点的标注不一致.数据点信息不准确... 噪声是针对整个输入空间的. 存在噪声的情况下,VC bound依旧有用: 存在噪声,就是f--->p(y|x),f是p的特殊情况:如 ...