PAT Grade _1039 Course List for Student (25 points)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44705116/article/details/102715302

Subject to the effect: Enter the course code and participated in the student, and students take courses output.

Cin string and said to time out ...... hashed string, vector student number of student after the course of the pressure input into a hash of course, the sort and prior to output;

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int getnum(string s) {
	int id=0;
	for (int i = 0; i < 3; i++) {
		id = id * 26 + s[i] - 'A';
	}
	id = id * 10 + s[3] - '0';
	return id;
}

const int maxn = 26 * 26 * 26 * 10 + 10;
vector<int> v[maxn];

int main() {
	int n, k, no, num, id;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < k; i++) {
		char name[9];
		scanf("%d%d", &no, &num);
		for (int j = 0; j < num; j++) {
			scanf("%s", name);
			id = getnum(name);
			v[id].push_back(no);
		}
	}
	for (int i = 0; i < n; i++) {
		char name[9];
		scanf("%s", name);
		int id = getnum(name);
		sort(v[id].begin(), v[id].end());
		printf("%s %d", name, v[id].size());
		for (int j = 0; j < v[id].size(); j++) {
			printf(" %d", v[id][j]);
		}
		printf("\n");
	}
	return 0;
}```

Guess you like

Origin blog.csdn.net/weixin_44705116/article/details/102715302