DNA Sorting(排序)
DNA SortingTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem 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. This problem contains multiple test cases! The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks. The output format consists of N output blocks. There is a blank line between output blocks. 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 (1 < 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''. If two or more strings are equally sorted, list them in the same order they are in the input file.
Sample Input
1 10 6 AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT
Sample Output
CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA
|
思路:从小到大排列。。。不好说,看题;
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct{
int num;
char str[];
}node;
int cmp(node a,node b){
return a.num<b.num;
}
int search(int n,char *a){int flot=;
for(int i=;i<n;++i){
for(int j=i+;j<n;++j){
if(a[j]-a[i]<)flot++;
}
}
return flot;
}
int main(){
int m,n,T;
node dna[];
scanf("%d",&T);
while(T--){scanf("%d%d",&n,&m);
int i=;
for(int i=;i<m;++i){
scanf("%s",dna[i].str);
dna[i].num=search(n,dna[i].str);
}
sort(dna,dna+m,cmp);
for(int i=;i<m;++i)printf("%s\n",dna[i].str);
}
return ;
}
快排:
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef struct{
int num;
char str[];
}node;
node dna[];
int search(int n,char *a){
int flot=;
for(int i=;i<n;++i){
for(int j=i+;j<n;++j){
if(a[j]-a[i]<)flot++;
}
}
return flot;
}
void Swap(node &a, node &b){
swap(a.num, b.num);
char s[];
strcpy(s, a.str);
strcpy(a.str, b.str);
strcpy(b.str, s);
}
int handle(int l, int r){
int j = l - ;
for(int i = l; i <= r; i++){
if(dna[i].num <= dna[r].num){
j++;
Swap(dna[i], dna[j]);
}
}
return j;
}
void quiksort(int l, int r){
int m;
if(l < r){
m = handle(l, r);
quiksort(l, m - );
quiksort(m + , r);
}
}
int main(){
int m,n,T; scanf("%d",&T);
while(T--){scanf("%d%d",&n,&m);
int i=;
for(int i=;i<m;++i){
scanf("%s",dna[i].str);
dna[i].num=search(n,dna[i].str);
}
quiksort(, m - );
for(int i=;i<m;++i)printf("%s\n",dna[i].str);
}
return ;
}
DNA Sorting(排序)的更多相关文章
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
- [POJ] #1007# DNA Sorting : 桶排序
一. 题目 DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 95052 Accepted: 382 ...
- DNA Sorting 分类: POJ 2015-06-23 20:24 9人阅读 评论(0) 收藏
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 88690 Accepted: 35644 Descrip ...
- 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: 95437 Accepted: 38399 Des ...
- [POJ 1007] DNA Sorting C++解题
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77786 Accepted: 31201 ...
- DNA Sorting POJ - 1007
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 114211 Accepted: 45704 De ...
- DNA Sorting
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 105159 Accepted: 42124 De ...
- 算法:POJ1007 DNA sorting
这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...
随机推荐
- JAVA 代理模式(Proxy)
1.代理模式 代理模式的作用是:为其他对象提供一种代理以控制对这个对象的访问.在某些情况下,一个客户不想或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式一般涉 ...
- J2EE之普通类载入web资源文件的方法
在WEB中普通类并不能像Servlet那样通过this.getServletContext().getResourceAsStream()获取web资源,须要通过类载入器载入,这里有两种方式,这两种方 ...
- oracle查询优化
1. 排序合理查询条件 Oracle自下而上分析顺序WHERE收条,从优化性能的角度.它建议,这些条件将能够过滤掉大量行书写的WHERE在条款结束, 之间的连接条件置于其它WHERE子句之前,即对易排 ...
- 使用WeCloud消息推送接口发送消息NodeJs版
WeCloud是一家初创公司的产品,眼下主要在做Android和IOS消息推送这块.他们提供了用于向设备发送消息的协议,详细协议内容见消息推送协议. 这篇文章将使用NodeJs基于这个推送协议完毕向A ...
- 在Docker中运行torch版的neural style
相关的代码都在Github上,请参见我的Github,https://github.com/lijingpeng/deep-learning-notes 敬请多多关注哈~~~ 在Docker中运行to ...
- 调试Linq的时候获得相对应的SQL
(query as System.Data.Objects.ObjectQuery).ToTraceString()
- htm跨域锚点定位
a.html <div class="title" id="aaaa">保险服务</div> b.html <div class= ...
- servlet乱码以及解决
// 浏览器提交的数据是000110011(码表中对应的<编码> )等东西. // 浏览器以什么<码表>打开浏览器(而空中浏览器使用的编码是:<meta http-eq ...
- asp.net 页面上传文件控件后台代码Request.Files获取不到
今天开发中遇到页面文件上传控件选择了文件,而后台Request.Files.Count取值为0,之前开发中遇到过几次,老是忘掉,今天记下来. html: <input type="fi ...
- Could not load file or assembly 试图加载格式不正确的程序
问题: 今天发布项目的时候遇到这个破问题,纳闷了好久,最后想起来自己改过程序生成的目标平台(原生成目标平台是Any CPU,被我改成了X86的). 解决方法: 改回原来的Any CPU 从新发布即可.