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 ...
随机推荐
- 注册表 ReadBool类型和 ReadInteger 的关系
function TRegistry.ReadBool(const Name: string): Boolean; begin Result := ReadInteger(Name) <> ...
- UVA 10294 等价类计数
题目大意: 项链和手镯都是若干珠子穿成的环形首饰,手镯可以旋转和翻转,但项链只能旋转,给n个珠子,t种颜色,求最后能形成的手镯,项链的数量 这里根据等价类计数的polya定理求解 对于一个置换f,若一 ...
- xcode6 ios launchimage
1.点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image" 2.右侧的勾选可以让你选择是否要对ipad,横屏,竖屏,以及低版本的ios系 ...
- PHP和.NET通用的加密解密函数类,均使用3DES加解密 .
以下为php代码 <PRE class=PHP name="code"> </PRE><PRE class=PHP name="code&q ...
- linux内核编译
1,进入内核源码树,如果是第一次编译,建议清理以下内核功能选择文件: make mrproper 2,删除前一次编译的残留文件: make clean 3,配置内核功能 make menuconfig ...
- typedef定义函数类型或函数指针
转载请标明出处: 最近在看redis的代码,发现了有关函数指针的部分,想把它记下来. 在redis中有类似下面的定义,利用typedef 定义了一个新的类型,这种类型是一个函数: typedef vo ...
- ssh原理
客户端向服务器端发出连接请求 服务器端向客户端发出自己的公钥 客户端使用服务器端的公钥加密通讯密钥然后发给服务器端 如果通讯过程被截获,由于窃听者即使获知公钥和经过公钥加密的内容,但不拥有私 ...
- lightoj1038
//Accepted 2860 KB 16 ms //概率 //对于n,假设n变成1的期望步数为p(n) //则p(n)=1/t*sum((1+p(d))) d|n //解得:p(n)=1/(t-1) ...
- IOS NSInvocation用法简介
IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...
- 分析一个嵌入payload的恶意.lnk文件
原文:https://isc.sans.edu/diary/Analyzis+of+a+Malicious+.lnk+File+with+an+Embedded+Payload/20763 We re ...