百练/ 2018研究生上机测试 B: 字符串排序 -- 近似题:百1007:DNA排序

题目来源:http://bailian.openjudge.cn/ss2018/A

1. 本题仍是求逆序对问题,用归并排序

2. C++风格的比较函数,用于algorithm::sort,stable_sort等

1007:DNA排序

总时间限制1000ms   内存限制65536kB

描述

现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序。

逆序对指的是字符串A中的两个字符A[i]A[j],具有i < j A[i] > A[j] 的性质。如字符串”ATCG“中,TC是一个逆序对,TG是另一个逆序对,这个字符串的逆序对数为2

 

输入

1行:两个整数nmn(0<n<=50)表示字符串长度,m(0<m<=100)表示字符串数量

2m+1行:每行是一个长度为n的字符串

输出

按逆序对数从少到多输出字符串,逆序对数一样多的字符串按照输入的顺序输出。

样例输入

10 6

AACATGAAGG

TTTTGGCCAA

TTTGGCCAAA

GATCAGATTT

CCCGGGGGGA

ATCGATGCAT

样例输出

CCCGGGGGGA

AACATGAAGG

GATCAGATTT

ATCGATGCAT

TTTTGGCCAA

TTTGGCCAAA

-----------------------------------------------------

解题思路

归并排序求每个字符串的逆序对数,再用algorithm::stable_sort关于逆序对数对输入的乱序字符串进行排序。

-----------------------------------------------------

代码

#include<fstream>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int counter = 0;

struct node {
    string s;
    int cnt;
};

// C++风格比较函数,用于algorithm::sort, stable_sort等
bool node_compare(struct node a, struct node b)
{
    return (a.cnt < b.cnt);
}

void Merge(string &s, int l, int mid, int r)
{
    vector<char> tmp;
    int i = l, j = mid+1;
    int rever = 0;                              // 记录当前统计到的逆序对个数
    while (i <= mid && j <= r)
    {
        if (s.at(i) <= s.at(j))
        {
            tmp.push_back(s.at(i++));
        }
        else
        {
            tmp.push_back(s.at(j));
            counter += j-i-rever;               // 核心代码:增加的逆序对数 = j-i-已有的逆序对数
            rever++;
            j++;
        }
    }
    while (i <= mid)
    {
        tmp.push_back(s.at(i++));
    }
    while (j <= r)
    {
        tmp.push_back(s.at(j++));
    }
    for (i=l; i<=r; i++)
    {
        s.at(i) = tmp.at(i-l);
    }
}

void MergeSort(string &s, int l, int r)
{
    if (l==r)
    {
        return;
    }
    else
    {
        int mid = (l+r)/2;
        MergeSort(s, l, mid);
        MergeSort(s, mid+1, r);
        Merge(s, l, mid, r);
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    ifstream fin("bailian1007.txt");
    int n,m,i,j;
    fin >> n >> m;
    vector<node> ans;
    for (i=0; i<m; i++)
    {
        string dna;
        fin >> dna;
        string dna1 = dna;
        counter = 0;
        MergeSort(dna1, 0, n-1);
        node mynode;
        mynode.s = dna;
        mynode.cnt = counter;
        ans.push_back(mynode);
    }
    stable_sort(ans.begin(), ans.end(), node_compare);  // algorithm::stable_sort,不会改变输入顺序; algorithm::sort可能会改变
    for (i=0; i<m; i++)
    {
        cout << ans.at(i).s << endl;
    }
    fin.close();
    #endif // LOCAL
    #ifdef ONLINE_JUDGE
    int n,m,i,j;
    cin >> n >> m;
    vector<node> ans;
    for (i=0; i<m; i++)
    {
        string dna;
        cin >> dna;
        string dna1 = dna;
        counter = 0;
        MergeSort(dna1, 0, n-1);
        node mynode;
        mynode.s = dna;
        mynode.cnt = counter;
        ans.push_back(mynode);
    }
    stable_sort(ans.begin(), ans.end(), node_compare);
    for (i=0; i<m; i++)
    {
        cout << ans.at(i).s << endl;
    }
    #endif // ONLINE_JUDGE
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值