原题链接

题目大意:给定一串字符串,查找字符串里字母逆序排列的对数,按照由少到多的顺序把所有字符串进行排列。

解法:用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的更多相关文章

  1. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

  2. poj 1007:DNA Sorting(水题,字符串逆序数排序)

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 80832   Accepted: 32533 Des ...

  3. [POJ1007]DNA Sorting

    [POJ1007]DNA Sorting 试题描述 One measure of ``unsortedness'' in a sequence is the number of pairs of en ...

  4. DNA Sorting 分类: POJ 2015-06-23 20:24 9人阅读 评论(0) 收藏

    DNA Sorting Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 88690 Accepted: 35644 Descrip ...

  5. poj 1007 (nyoj 160) DNA Sorting

    点击打开链接 DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 75164   Accepted: 30 ...

  6. [POJ] #1007# DNA Sorting : 桶排序

    一. 题目 DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95052   Accepted: 382 ...

  7. poj 1007 DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95437   Accepted: 38399 Des ...

  8. DNA Sorting(排序)

    欢迎参加——BestCoder周年纪念赛(高质量题目+多重奖励) DNA Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

  9. [POJ 1007] DNA Sorting C++解题

        DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 77786   Accepted: 31201 ...

随机推荐

  1. myeclipse9.x,10.x 安装时出现pulse-one-64,failed to load the JNI shared library

  2. sqlite3 SQL常用语句

    1. select SELECT LastName,FirstName FROM Persons; SELECT * FROM Persons; 2. where SELECT * FROM Pers ...

  3. MBProgressHUD.h file not found

    MBProgressHUD框架,怎么我导入MBProgressHUD+MJ.h会报错.(即MBProgressHUD+MJ根本不存在),我看其他人的视屏又可以导入 MBProgressHUD.h fi ...

  4. JS 原型继承的几种方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Linksys WRT120N路由器备份文件解析

    Perusing the release notes for the latest Linksys WRT120N firmware, one of the more interesting comm ...

  6. unity3d基础02

    调试: 在MonoDevelop里可以断点调试,注意绑定unity进程 使用Debug.Log()打印信息 创建游戏对象: GameObject test = GameObject.CreatePri ...

  7. Python网络编程03----Python3.*中socketserver

    socketserver(在Python2.*中的是SocketServer模块)是标准库中一个高级别的模块.用于简化网络客户与服务器的实现(在前面使用socket的过程中,我们先设置了socket的 ...

  8. KeySweeper 微软无线键盘嗅探装置

    Author:Samy Kamkar From: http://samy.pl/keysweeper/ 中文字幕由Galaxy无名提供 PS:视频传到youku之后发现50秒左右的字幕被干掉了,不知道 ...

  9. 简单学习:repo入门

    一:关于repo repo是Google开发的用于管理Android版本库的一个工具,repo并不是用于取代git,而是用Python对git进行了一定的封装,简化了对多个Git版本库的管理.对于re ...

  10. python3爬虫再探之EXCEL(续)

    上篇介绍了xlsxwriter的用法,本来想写一下xlrd和xlwt的用法,看到这篇文章——http://blog.csdn.net/wangkai_123456/article/details/50 ...