您的位置:首页 > 其它

UVALive 7370--字符串排序

2016-08-19 20:17 393 查看
题目来源:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: