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 ...
随机推荐
- ubuntu下php安装xdebug
1.安装 sudo apt-get install php5-xdebug 2.配置 修改 php .ini 路径: /etc/php5/apache2/php.ini (这里可能有不同,不同的u ...
- Android将第三方jar打包进apk
转自:http://blog.csdn.net/liushaogeng/article/details/6641704 使用第三方jar包 除了我以下介绍的方法,别的方法我基本上都试验过,均会出现编译 ...
- java类加载器行为[笔记]
1. Java虚拟机使用第一个类的第一件事情就是将该类的字节码装进来,装载类字节码的功能是由类装器完成的,类半遮器负责根据一个类的名称来定位和生成类的字节码数据后返回给Java虚拟机. 2. 类装载器 ...
- android面试题之四
十六.Android中Dalvik和JVM的区别是什么? 1. Dalvik基于寄存器,而JVM基于栈.基于寄存器的虚拟机对于更大的程序来说,在它们编译的时候,花费的时间更短. 2. Dalvik负责 ...
- Docker image 镜像介绍
操作镜像 使用 docker 命令行操作 docker 镜像 获取镜像 使用「docker pull +镜像名称」从网络上下载image镜像 core@localhost ~ $ docker pul ...
- Tomcat 常见问题篇
Tomcat 常见问题一.Tomcat常见问题 1.Tomcat web容器出现故障时,我们通过Tomcat自带的logs查看原因,以下错误提示都是源于logs 2.Connection refuse ...
- 前端判断用户请求是PC还是移动端
链接:https://www.zhihu.com/question/20004700/answer/13678113 第一步先在服务器端使用User Agent判断,先匹配出移动设备,这一步可以统计U ...
- 探究foreach对于迭代变量的封装性的研究
众所周知教科书上对于foreach之中的注释是在遍历过程中无法改变其遍历的元素例如声明一个数组 ,,,}; foreach(int m in ii){ m = ;//错误 “m”是一个“foreach ...
- Xcode的代码片段快捷方式-Code Snippet Library(代码片段库)
最近换了新电脑,装上Xcode敲代码发现很多以前攒的Code Snippet忘记备份了,总结了一下Code Snippet的设置方法,且行且添加,慢慢积累吧. 如下图: Title - Code ...
- (原)使用vectot的.end()报错:iterators incompatible
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5070672.html 参考网址: http://blog.csdn.net/yxnyxnyxnyxny ...