您的位置:首页 > 编程语言 > C语言/C++

zoj 1188 DNA Sorting(STL)

2015-07-19 09:50 381 查看
  题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=188

  题       意:给你几个字符串,按他们的倒位数量对他们进行排序;

                            倒位数量等于字符串中的每一位字符的位于他后面且比他小的字符的个数之和。

                        例:    在字母序列“DAABEC”中,“没排序”的尺度是5,因为D 比它右边的4 个字母大,而E 比它右边的1 个字母大。

                               这个尺度被称为序列的倒位数量。

 思        路:将倒位数量作为关键字对字符排序。

 代码如下:

#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp( const string &s1, const string &s2 )
{
int c1=0;
for( int i = 0; i < s1.size(); i ++ )//计算s1的倒位数量
for( int j = i + 1; j < s1.size(); j ++ )
if( s1[i] > s1[j] )
c1++;
int c2=0;
for( int i = 0; i < s2.size(); i ++ )//计算s2的倒位数量
for( int j = i + 1; j < s2.size(); j ++ )
if( s2[i] > s2[j] )
c2++;
return c1<c2;
}
int main()
{
int T;
scanf ( "%d", &T );
int ans = 1;
while ( ans <= T )
{
int n, m;
scanf ( "%d %d", &n, &m );
vector<string>v;
v.clear();//清空数组
for( int i = 0; i < m; i ++ )
{
string ss;
cin>>ss;
v.push_back(ss);
}
sort( v.begin(), v.end(), cmp );
for( int i = 0 ; i < v.size(); i ++ )
{
cout<<v[i]<<endl;
}
if( ans < T ) printf("\n");//输出空行
ans++;
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息