Zhejiang University has 40000 students and provides 2500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=40000), the total number of students, and K (<=2500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (<=20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.
Output Specification:
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=40000), the total number of students, and K (<=2500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (<=20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.
Output Specification:
For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.
IDEA
1.超时问题:a.不能用string,需要用char [],排序sort比较时用strcmp;b.不能用cin,cout需要用scanf,printf。
二者如果有一个不满足都会造成超时。
2.struct Name{
char str[5];
};
vector< vector<Name> > vec(k);
vector中不能直接存char [],则先声明结构体Name,里面放char [] ,再将Name存进来就可是实现了
CODE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<fstream>
using namespace std;
#define len 4
struct Name{
char str[5];
};
int cmp(Name name1,Name name2){
// for(int i=0;i<len;i++){
// if(name1.str[i]==name2.str[i]){
// continue;
// }
// return name1.str[i]<name2.str[i];
// }
return strcmp(name1.str,name2.str)<0;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
#endif
int n,k;
//cin>>n>>k;
scanf("%d %d",&n,&k);
vector< vector<Name> > vec(k);
for(int i=0;i<n;i++){
//string name;
//cin>>name;
Name name;
scanf("%s",name.str);
int c;
//cin>>c;
scanf("%d",&c);
for(int j=0;j<c;j++){
int course;
//cin>>course;
scanf("%d",&course);
vec[course-1].push_back(name);
}
}
for(int i=0;i<k;i++){
//cout<<i+1<<" "<<vec[i].size()<<endl;
printf("%d %d\n",i+1,vec[i].size());
sort(vec[i].begin(),vec[i].end(),cmp);
for(int j=0;j<vec[i].size();j++){
//cout<<vec[i][j].str<<endl;
printf("%s\n",vec[i][j].str);
}
}
#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}