字符串排序

Description:

有一些A、C、M组成的字符串,将其按字符A排序。

Input:

一组测试数据,输入数据由若干行组成,每行是字符A、C或M组成的字符串。(每个字符串长度不超过1200)

Output:

对所有输入的数据,先按字符A的个数进行升序排序,如果字符A的数量相等,再按出现的先后顺序排序,每行输出一个字符串。

Sample Input:

ACM
MCA
AACAAMMM
AACCMM
CMAAMMMMMM
AAA

Sample Output:

ACM
MCA
AACCMM
CMAAMMMMMM
AAA
AACAAMMM

Note:

本题由旧版NOJ导入,来源:NUPT

 

#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <climits>
#include <stdio.h>
#include <iomanip>
#include <math.h>   
#include <list>
#include <queue>
#include <tuple> 
#include <stack>
#include <string.h>
using namespace std;

typedef struct 
{
    string name;
    long long index;
}mystruct;
bool mycmp(const mystruct& A, const mystruct& B)
{
    long long countA = 0;
    long long countB = 0;
    for(int i = 0; i < A.name.size(); i++)
    {
        if(A.name[i] == 'A')
            countA++;
    }
    for(int i = 0; i < B.name.size(); i++)
    {
        if(B.name[i] == 'A')
            countB++;
    }
    if(countA < countB)
        return true;
    else if(countA > countB)
        return false;
    if(A.index < B.index)
        return true;
    return false;
}
int main(void)
{
    vector<mystruct> nums;
    string tmp;
    long long id = 0;
    while(cin >> tmp)
    {
        mystruct tmp1;
        tmp1.name = tmp;
        tmp1.index = id;
        nums.push_back(tmp1);
        id++;
    }
    sort(nums.begin(), nums.end(), mycmp);
    for(int i = 0; i < nums.size(); i++)
    {
        cout << nums[i].name<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值