ZOJ 1188 DNA Sorting
题目大意:给定一串字符串,查找字符串里字母逆序排列的对数,按照由少到多的顺序把所有字符串进行排列。
解法:用C++字符串string类的iterator,从每个字符串的起始开始,查找逆序字符的个数,然后用qsort方法按照reverseCount的大小快速排序。
参考代码:
#include<iostream>
#include<string>
#include<string.h>
#include<cstdlib>
#include<cstdio> using namespace std; struct DNAStr{
int index,reverseCount;
string str;
}DNA[102]; int countReverse(string s){
int num = 0;
string::iterator lit = s.begin();
string::iterator rit;
for(; lit < s.end(); lit++)
for(rit = lit + 1; rit <s.end(); rit++)
{
if(*lit > *rit)
{
num++;
}
}
return num;
}
int cmp(const void *a, const void *b){
DNAStr * x = (DNAStr *)a;
DNAStr * y = (DNAStr *)b; return (DNAStr *)x->reverseCount > (DNAStr *)y->reverseCount;
} int main(){
int cas,m,n; cin>>cas;
while(cas--){
getchar();
cin>>n>>m;
getchar();
for(int i=0;i<m;i++){
getline(cin,DNA[i].str);
DNA[i].index=i;
} for(int i=0;i<m;i++)
DNA[i].reverseCount = countReverse(DNA[i].str);
qsort(DNA,m,sizeof(DNAStr),cmp); for(int i=0;i<m;i++){
cout<<DNA[i].str<<endl;
}
if(cas)
cout<<endl; } return 0;
}
ZOJ 1188 DNA Sorting的更多相关文章
- 算法:POJ1007 DNA sorting
这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...
- poj 1007:DNA Sorting(水题,字符串逆序数排序)
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 80832 Accepted: 32533 Des ...
- [POJ1007]DNA Sorting
[POJ1007]DNA Sorting 试题描述 One measure of ``unsortedness'' in a sequence is the number of pairs of en ...
- 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: 95052 Accepted: 382 ...
- poj 1007 DNA Sorting
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 95437 Accepted: 38399 Des ...
- DNA Sorting(排序)
欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) DNA Sorting Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- [POJ 1007] DNA Sorting C++解题
DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 77786 Accepted: 31201 ...
随机推荐
- PHPExcel 学习笔记
首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...
- for循环往Oracle中插入n条数据,主键自增
1.主键自增实现方法:http://www.cnblogs.com/Donnnnnn/p/5959871.html 2.for循环往Oracle中插入n条数据 BEGIN .. loop insert ...
- cmd的xcopy命令
C#项目的PostEvent里经常会用到xcopy命令,复制目录时容易出错,如下: xcopy sourceDir targetDir,其中的2个目录最后不能有反斜杠"",而目录类 ...
- 编绎openssl杂记(window)
Window 下 OpenSSL 编绎过程 1. 下载 ActivePerl-5.12.4.1205 , openssl-0.9.8 , 配置Perl环境变量 , 解压openssl-0.9.82. ...
- Servlet下
HTTP简介 HTTP是 hypertext transfer protocol(超文本传输协议)的简写,它是 TCP/IP 协议集中的一个应用层协议,用于定义WEB浏览器与WEB服务器之间交换数据的 ...
- Linux-编译器gcc/g++编译步骤
gcc和g++现在是gnu中最主要和最流行的c&c++编译器.g++是c++的命令,以.cpp为主:对于c语言后缀名一般为.c,这时候命令换做gcc即可.编译器是根据gcc还是g++来确定是按 ...
- java 基本类库包的作用
tools.jar:工具类库,它跟我们程序中用到的基础类库没有关系. Jre库包含的jar文件(jdk1.6):resources.jar.rt.jar.jsse.jar.jce.jar.charse ...
- 关于equals和hashCode
equals()和hashCode()是Object类的两个函数,重要性可见一斑,不过我们平时使用却未必能深入理解他们.本文从java doc触发,讲到它们与哈希表的关系,再到具体的实现,就我目前掌握 ...
- PL/SQL : Procedural Language / Structual Query Language and it is an exrension to SQL.
SQL is not very flexible and it cannot be made to react differently to differing sutuations easily. ...
- 自定义圆的半径layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:too ...