POJ 1007 DNA Sorting

来源:互联网 发布:java 暂停1秒 编辑:程序博客网 时间:2024/06/05 14:54

用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;
}

0 0
原创粉丝点击