UVALive 7370--字符串排序

题目来源:

UVALive 7370

题意:

给 n 个名字,每个名字后面都带着来自哪个班级,班级的优先级是: upper>middle>lower,根据班级的优先级将名字进行排序,优先级

高的班级名字在前面,若班级的优先级相同,则根据名字的字典序从小到大进行排列。注意:班级的优先级是从后面往前比较,如果

优先级说明不够长,则省下的在优先级前面补Middle。


分析:

这是一个考验审题的字符串处理的题目,只要将题目中的排序规则读出来就好弄了,另外有一个小技巧就是:将优先级说明的输入转

换成连续的输入字符串,直到遇到class的时候截止,这也是今天新学到的东西,之后就是将三个优先级的说明分别转换成1,2,3,找出

数目最多的说明,然后将剩下的短的说明补全,补miidle,之后逆序比较就可以了,注意比较的时候依然选择结构体比较。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<string>
#include<cmath>
#include<vector>
using namespace std;

struct node
{
    char name[300];
    char s[300];
    int num;
} m[1005];

bool cmp(const node &a,const node &b)
{
    if(strcmp(a.s,b.s)>0)
        return true;
    if(strcmp(a.s,b.s)==0&&strcmp(a.name,b.name)<0)
        return true;
    return false;
}
int main()
{
    int n;
    char ss[300];
    while(~scanf("%d",&n))
    {
        int mmax=0;
        for(int i=0; i<n; i++)
        {
            scanf("%s",m[i].name);
            int len=strlen(m[i].name);
            m[i].name[len-1]=0;
            int cnt=0;
            while(~scanf("%s",ss)&&strcmp(ss,"class")!=0)
            {
                if(ss[0]=='u')
                    m[i].s[cnt++]='3';
                if(ss[0]=='m')
                    m[i].s[cnt++]='2';
                if(ss[0]=='l')
                    m[i].s[cnt++]='1';
            }
            for(int j=0; j<cnt/2; j++)
                swap(m[i].s[j],m[i].s[cnt-1-j]);
            m[i].num=cnt;
            mmax=max(cnt,mmax);
        }
        for(int i=0; i<n; i++)
        {
            if(m[i].num!=mmax)
            {
                for(int j=m[i].num; j<mmax; j++)
                    m[i].s[j]='2';
            }
            m[i].s[mmax]=0;
        }
        sort(m,m+n,cmp);
        for(int i=0; i<n; i++)
            printf("%s\n",m[i].name);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值