A1153 Decode Registration Card of PAT (25)

分解PAT字符串进行不同部分统计
使用cin,cout,string超时

1. 原文

A registration card number of PAT consists of 4 parts:

  • the 1st letter represents the test level, namely, T for the top level, A for advance and B for basic;
  • the 2nd - 4th digits are the test site number, ranged from 101 to 999;
  • the 5th - 10th digits give the test date, in the form of yymmdd;
  • finally the 11th - 13th digits are the testee’s number, ranged from 000 to 999.

Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤104) and M (≤100), the numbers of cards and the queries, respectively.

Then N lines follow, each gives a card number and the owner’s score (integer in [0,100]), separated by a space.

After the info of testees, there are M lines, each gives a query in the format Type Term, where

  • Type being 1 means to output all the testees on a given level, in non-increasing order of their scores. The corresponding Term will be the letter which specifies the level;
  • Type being 2 means to output the total number of testees together with their total scores in a given site. The corresponding Term will then be the site number;
  • Type being 3 means to output the total number of testees of every site for a given test date. The corresponding Term will then be the date, given in the same format as in the registration card.

Output Specification:

For each query, first print in a line Case #: input, where # is the index of the query case, starting from 1; and input is a copy of the corresponding input query. Then output as requested:

  • for a type 1 query, the output format is the same as in input, that is, CardNumber Score. If there is a tie of the scores, output in increasing alphabetical order of their card numbers (uniqueness of the card numbers is guaranteed);
  • for a type 2 query, output in the format Nt Ns where Nt is the total number of testees and Ns is their total score;
  • for a type 3 query, output in the format Site Nt where Site is the site number and Nt is the total number of testees at Site. The output must be in non-increasing order of Nt‘s, or in increasing order of site numbers if there is a tie of Nt.

If the result of a query is empty, simply print NA.

Sample Input:

1
2
3
4
5
6
7
8
9
10
11
12
13
8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

Sample Output:

1
2
3
4
5
6
7
8
9
10
11
12
Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

2. 解析

14位字符串分别提取不同部分进行统计

  • ABT级别 level
  • 考场 site
  • 年份 year

📝记录输入值 stu[i] –> node(char id[15],int grade) 数组

根据查询提取不同id部分

  • level: id[0]==level[0]

    分数从大到小排,相同时按字母排 – sort

  • 对应site的数量++,分数+=grade 存入map中

  • 需要提交5-10位的year,2-4位的site –>map.vector

3. AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<cstdio>
#include<map>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
char str[20]; int n;
map<int,int> cnt;
map<int,int> score;
struct node
{
char id[20];
int grade;
}stu[10010];
bool cmp(node a,node b){
if(a.id[0]!=b.id[0]){
return a.id[0]<b.id[0];
}
else if (a.grade!=b.grade)
{
return a.grade>b.grade;
}else{
return strcmp(a.id,b.id)<0;
}
}
struct node2
{
int site,cnt;
};
bool cmp2(node2 a,node2 b){
if (a.cnt!=b.cnt)
{
return a.cnt>b.cnt;
}else{
return a.site<b.site;
}
}
void dealevel(char c)
{
int count=0;
for (int i = 0; i < n; ++i)
{
if (stu[i].id[0]==c)
{
printf("%s %d\n",stu[i].id,stu[i].grade);
count++;
}
}
if (count==0)
{
printf("NA\n");
}
}
void dealsite(char c[])
{
int temp=0;
for (int i = 0; i < 3; ++i)
{
temp=temp*10+c[i]-'0';
}
if (cnt[temp]!=0)
{
printf("%d %d\n",cnt[temp],score[temp]);
}else{
printf("NA\n");
}
}
void dealyear(char c[])
{
map<int,int> sites;
vector<node2> vTemp;
int year=0; int flag=0;
for (int j = 0; j < 6; ++j)
{
year=year*10+c[j]-'0';
}
for (int i = 0; i < n; ++i)
{
int id=0;
for (int j = 4; j < 10; ++j)
{
id=id*10+stu[i].id[j]-'0';
}
//printf("%s %s\n",id.c_str(),year.c_str());
if (id==year)
{
flag=1;
int ans=0;
for (int j = 1; j < 4; ++j)
{
ans=ans*10+stu[i].id[j]-'0';
}
sites[ans]++;
}
}
if (flag==0)
{
printf("NA\n");
}else{
node2 temp;
for (map<int,int>::iterator i = sites.begin(); i != sites.end(); ++i)
{
temp.site=i->first;
temp.cnt=i->second;
vTemp.push_back(temp);
}
sort(vTemp.begin(),vTemp.end(),cmp2);
for (int i = 0; i < vTemp.size(); ++i)
{
printf("%d %d\n",vTemp[i].site,vTemp[i].cnt);
}
}
}
int main()
{
int m;
scanf("%d%d",&n,&m);
node temp;
for (int i = 0; i < n; ++i)
{
scanf("%s%d",temp.id,&temp.grade);
stu[i]=temp;
int site=0;
for (int j = 1; j < 4; ++j)
{
site=site*10+temp.id[j]-'0';
}
//printf("%s\n",site.c_str());
cnt[site]++;
score[site]+=temp.grade;
}
sort(stu,stu+n,cmp);
int idx;
for (int i = 1; i <= m; ++i)
{
scanf("%d%s",&idx,str);
printf("Case %d: %d %s\n",i,idx,str);
if (idx==1)
{
dealevel(str[0]);
}else if (idx==2)
{
dealsite(str);
}else if (idx==3)
{
dealyear(str);
}else{
printf("NA\n");
}
}
return 0;
}

使用cin,cout,string 出现超时
使用char[] 数据的提取转为int存储

1
2
3
4
5
int a=0;
for (int j = 1; j < 4; ++j)
{
a=a*10+id[j]-'0';
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/02/A1153/
  • 发布时间: 2019-09-02 08:57
  • 更新时间: 2022-06-01 22:46
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

Gitalking ...