POJ 1007 DNA Sorting
2015-10-14 11:44
375 查看
用STL封装的排序算法和自定义排序规则来进行字符串排序。
// codingEx.cpp : Defines the entry point for the console application.
// author : momogary1204
//#include "stdafx.h"
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <algorithm>
using namespace std;
int sortStepLen(const string& input);
bool less_second(const pair<int, int>& m1, const pair<int, int>& m2){
return m1.second < m2.second;
}
const int MAX_LEN = 50;
const int MAX_CASE = 100;
int main()
{
int strLen = 0, strNum = 0;
cin >> strLen >> strNum;
string array[MAX_CASE];
//int sortDist[MAX_CASE];
vector<pair<int, int>> tmp;//pair对实现字符串和其乱序个数对应
for (int i = 0; i<strNum; i++){
cin >> array[i];
tmp.push_back(pair<int, int>(i, sortStepLen(array[i])));
//cout << sortDist[i] << ' ';
}
//STL封装的排序函数,第三个参数系统有自己的less仿函数,注意自定义比较函数的命名,前两个参数是范围的随机访问迭代器
sort(tmp.begin(), tmp.end(), less_second);
for (int i = 0; i < strNum; i++){
cout << array[tmp[i].first] << endl;
}
return 0;
}
//计算乱序对的个数
int sortStepLen(const string& input){
int len = input.length();
int stepLen = 0;
for (int i = 0; i<len; i++){
for (int j = i + 1; j<len; j++){
if (input[i]>input[j]){
stepLen += 1;
}
}
}
return stepLen;
}
// codingEx.cpp : Defines the entry point for the console application.
// author : momogary1204
//#include "stdafx.h"
#include<iostream>
#include<map>
#include<string>
#include<vector>
#include <algorithm>
using namespace std;
int sortStepLen(const string& input);
bool less_second(const pair<int, int>& m1, const pair<int, int>& m2){
return m1.second < m2.second;
}
const int MAX_LEN = 50;
const int MAX_CASE = 100;
int main()
{
int strLen = 0, strNum = 0;
cin >> strLen >> strNum;
string array[MAX_CASE];
//int sortDist[MAX_CASE];
vector<pair<int, int>> tmp;//pair对实现字符串和其乱序个数对应
for (int i = 0; i<strNum; i++){
cin >> array[i];
tmp.push_back(pair<int, int>(i, sortStepLen(array[i])));
//cout << sortDist[i] << ' ';
}
//STL封装的排序函数,第三个参数系统有自己的less仿函数,注意自定义比较函数的命名,前两个参数是范围的随机访问迭代器
sort(tmp.begin(), tmp.end(), less_second);
for (int i = 0; i < strNum; i++){
cout << array[tmp[i].first] << endl;
}
return 0;
}
//计算乱序对的个数
int sortStepLen(const string& input){
int len = input.length();
int stepLen = 0;
for (int i = 0; i<len; i++){
for (int j = i + 1; j<len; j++){
if (input[i]>input[j]){
stepLen += 1;
}
}
}
return stepLen;
}
相关文章推荐
- wireshark cannot find interface
- ArcGIS教程:值提取至点 (空间分析)
- extension=php_phalcon.dll 安装过程
- Mysql修改&设置密码
- Linux多线程信号量的概念和使用
- yacc 和 lex 在ubuntu 上安装
- jQuery MiniUI开发系列之:创建组件对象
- SUSE10 SP4源码升级Python到2.6.6
- C# String.Format大全
- 指针设计类和对象回应程序
- HTML5 文档类型
- hdu4777 Rabbit Kingdom 离线树状数组 求询问区间内的区间数
- Struts2中的struts.multipart.saveDir的配置
- android中横竖屏切换状态保持一致
- 我感我悟。
- [原]C# 读取Word 表格数据(单元格纵合并)
- 淘宝穿衣搭配算法_方案三
- Yii数据库操作增删改查-[增加\查询\更新\删除 AR模式]
- mysql 按照 where id IN (42,25,26,27) 按照顺序查找id 的办法
- maven的一些问题记录