题目链接
题目分析
PAT. A1039的逆过程
解题思路
(一):
set<string>[]
:以课程号为下标。最后一组测试点超时
(二):
用char[][]
+ vector
+ sort
输出时,在每个课程内部先排序 (排序时,用下标排序,而不是字符串本身)
小技巧:
如果排序是直接对字符串排序,那么会导致大量的字符串移动,非常消耗时间。因此比较合适的做法是使用 字符串的下标 来代替字符串本身进行排序
AC程序
/**************************
//@Author: 3stone
//@ACM: PAT-A1047
//@Time: 18/1/27
//@IDE: VS2017
***************************/
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define maxSize 40010
using namespace std;
char name[maxSize][5];
vector<int> cour[2510];
bool cmp(int s1, int s2) {
return strcmp(name[s1], name[s2]) < 0;
}
int main() {
int stuNum, subNum;
scanf("%d%d", &stuNum, &subNum);
for (int i = 1; i <= stuNum; i++) {//获取选课信息
int total, sub; //选课人数 & 课程编号
scanf("%s%d", name[i], &total);
for (int j = 0; j < total; j++) {
scanf("%d", &sub);
cour[sub].push_back(i); //学生编号放入课程记录中
}
}
for (int i = 1; i <= subNum; i++) { //output
sort(cour[i].begin(), cour[i].end(), cmp);
int count = cour[i].size();
printf("%d %d\n", i, count);
if (count == 0) continue;
else {
vector<int>::iterator tempEnd = cour[i].end();
for (vector<int>::iterator it = cour[i].begin(); it != tempEnd; it++) {
printf("%s\n", name[(*it)]);
}
}
}
system("pause");
return 0;
}
【set<string>[]
版本】(超时)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<set>
#include<map>
#define maxn 2505
using namespace std;
set<string> stu_of_course[maxn];
bool flag_of_course[maxn];
int main() {
int n, k, course_id, course_num;
string cur_stu;
scanf("%d%d", &n, &k);
memset(flag_of_course, false, sizeof(flag_of_course));
//逐个输入学生课程表
for (int i = 0; i < n; i++) {
cin >> cur_stu; //学生姓名
scanf("%d", &course_num); //所选课程数量
for(int j = 0; j < course_num; j++) {
scanf("%d", &course_id);
stu_of_course[course_id].insert(cur_stu);
flag_of_course[course_id] = true;
}
}
for (int i = 1; i <= k; i++) {
if (stu_of_course[i].size() == 0) {
printf("%d 0\n", i);
} else {
printf("%d %d\n", i, stu_of_course[i].size());
set<string>::iterator set_end = stu_of_course[i].end();
for (set<string>::iterator it = stu_of_course[i].begin(); it != set_end; it++) {
cout << *it << endl;
}
}
}
system("pause");
return 0;
}