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;
}
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;
}
相关文章推荐
- UVaLive 7370 Classy (排序,比较)
- POJ2945 UVALive3527 Find the Clones【字符串排序】
- uva 10815 Andy's First Dictionary(字符串处理+sort排序)
- UVALive 4513 Stammering Aliens 字符串Hash求LCS
- UVALive 6609 Minimal Subarray Length (查找+构建排序数组)
- [UVALive3942] Remember the Word && 字符串
- UVA 156-Ananagrams(字符串排序按序输出无重复单词)
- UVALive 6047 Perfect Matching(字符串哈希)#by zh
- UVALive 3026 (LA 3026) Period KMP求字符串周期
- UVA 156 Ananagrams 关于二维数组表示的字符串排序的问题
- uva 123 Searching Quickly(字符串排序处理)
- UVa 1585 / UVALive 3354 Score(字符串的动态规划)
- UVALive 4513 Stammering Aliens 字符串Hash求LCS
- UVALive 2755 Hidden Password(字符串最小表示)
- UVa 400 Unix ls (字符串排序&规格化输出)
- UVALive 6604 Airport Sort 【归并排序】【逆序数】
- uva 10194 Football (aka Soccer)(字符串处理+sort排序)
- uvalive 5734 字符串最小表示法
- uva10132 字符串数组的qsort排序技巧
- UVA - 10905 Children's Game 字符串的排序