zoukankan      html  css  js  c++  java
  • 51nod 1874 字符串排序

    基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
     收藏
     关注
    定义一个字符串的无序度为所有位置后面的字母比该位置的字母小的总数之和。比如"DAABEC''这个字符串的无序度是5,因为D后面有4个位置比它小(AABC),E后面有1个比它小(C),其它位置后面没有比自己小的。" AACEDGG "的无序度为1(E后面有一个D比它小)。" ZWQM "的无序度为6,每个位置后面所有的字母都比它小。
    现在你的任务是给定一些字符串(只由大写字母组成),把他们按照无序度从小到大排序,如果无序度一样,那么就按照输入的相对顺序排序。
    Input
    单组测试数据。
    第一行有两个整数n(0 < n <= 50)和m (0 < m <= 100),分别表示输入的字符串的长度和字符串的个数。
    接下来m行,每一行包含一个长度为n的字符串,只由大写字母组成。
    Output
    输出m行,表示排序之后的字符串。
    Input示例
    10 6
    AACATGAAGG
    TTTTGGCCAA
    TTTGGCCAAA
    GATCAGATTT
    CCCGGGGGGA
    ATCGATGCAT
    Output示例
    CCCGGGGGGA
    AACATGAAGG
    GATCAGATTT
    ATCGATGCAT
    TTTTGGCCAA
    TTTGGCCAAA

    思路:使用vector+pair的搭配,排序函数很久没写了,有些生疏,查看了以前写的博客!


    #include <algorithm>
    #include <iostream>
    #include <sstream>      //stringstream
    #include <iomanip>      //cout输出小数格式
    #include <utility>       //pair
    #include <bitset>       //bitset
    #include <string>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <cstring>
    #include <cstdlib>
    #include <climits>      //limit
    #include <cstdio>
    #include <cmath>
    #include<utility>
    #include<string.h>
    using namespace std;
    
    
    string str[105];
    
    bool judge(const pair<int,int>a,const pair<int,int>b)
       {
           if(a.second!=b.second)
           return a.second<b.second;
           else
            return a.first<b.first;
       }
    int countStr(string str)
    {
        int cnt=0;
        for(int i=0;i<str.length();i++)
        {
            for(int j=i+1;j<str.length();j++)
            {
                if(str[i]>str[j])
                  cnt++;
            }
        }
        return cnt;
    }
    int main()
    {
       int n,m;//n=length,m=个数
       cin>>n>>m;
       vector <pair<int,int> >vec;
       for(int k=0;k<m;k++)
       {
           cin>>str[k];
           int strCnt=countStr(str[k]);
           vec.push_back(make_pair(k,strCnt));
       }
       sort(vec.begin(),vec.end(),judge);
       for(int i=0;i<vec.size();i++)
       {
           cout<<str[vec[i].first]<<endl;
       }
    
    
    
        return 0;
    }
    




  • 相关阅读:
    有一种努力叫“凌晨四点”
    编程思想
    小记
    团队精神与集体主义
    变量起名
    软件项目估量方法
    戏说QQ
    压力说
    AngularJS指令基础(一)
    Leetcode 1021. Best Sightseeing Pair
  • 原文地址:https://www.cnblogs.com/bryce1010/p/9387051.html
Copyright © 2011-2022 走看看