Pat(Advanced Level)Practice--1039(Course List for Student)

Pat1039代码

题目描述:

Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student‘s name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.

Sample Input:
11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9
Sample Output:
ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0
两种解题方法:
方法一:Hash表
AC代码:
#include<cstdio>
#include<cstdlib>
#define M 26*26*26*10
#define N 2500
typedef struct node
{
	int num;
	struct node *next;
}Node;

Node s[M];
char str[M][5];
int arr[N];
int comp(const void *a,const void *b);

unsigned int Hash(char *s) 
{
	unsigned int base;
	base=(s[0]-‘A‘)*26*26*10+(s[1]-‘A‘)*26*10
	+(s[2]-‘A‘)*10+s[3]-‘0‘;
	return base;
}

int main(int argc,char *argv[])
{
	int n,k;
	int i,j;
	unsigned int index;
	Node *p;
	int cnum,snum;
	char name[5];
	int count=0;
	scanf("%d%d",&n,&k);
	for(i=1;i<=M-1;i++)
	{
		s[i].num=0;
		s[i].next=NULL;													
	}																
	for(j=1;j<=k;j++)
	{
		scanf("%d%d",&cnum,&snum);
		getchar();
		for(i=1;i<=snum;i++)
		{
			scanf("%s",name);
			index=Hash(name);
			s[index].num++;
			p=(Node *)malloc(sizeof(Node));
			p->num=cnum;
			p->next=s[index].next;
			s[index].next=p;
		}
	}
	getchar();
	for(j=1;j<=n;j++)
		scanf("%s",str[j]);
	for(j=1;j<=n;j++)
	{
		count=0;
		index=Hash(str[j]);
		printf("%s %d",str[j],s[index].num);
     	if(s[index].num!=0)
		{
			p=s[index].next;
			while(p)
			{
				arr[count++]=p->num;                
				p=p->next;
			}
			qsort(arr,count,sizeof(int),comp);
			for(i=0;i<count;i++)
				printf(" %d",arr[i]);
			printf("\n");
		}
		else 
			printf("\n");
	}
	return 0;
}

int comp(const void*a,const void*b)
{
	return (*(int *)a-*(int *)b);
}

方法二:vector二维数组
AC代码:
#include<cstdio>
#include<vector>
#include<algorithm>

using namespace std;

int main(int argc,char *argv[])
{
	vector<vector<int> > stu(26*26*26*10);
	int N,K;
	int i,j,m;
	int base,index;
	char name[5];
	scanf("%d%d",&N,&K);
	for(i=0;i<K;i++)
	{
		scanf("%d%d",&index,&m);
		getchar();
		for(j=0;j<m;j++)
		{
			scanf("%s",name);
			base=(name[0]-‘A‘)*26*26*10+(name[1]-‘A‘)*26*10
			+(name[2]-‘A‘)*10+name[3]-‘0‘;
			stu[base].push_back(index);
		}
	}
	for(i=0;i<N;i++)
	{
		scanf("%s",name);
		base=(name[0]-‘A‘)*26*26*10+(name[1]-‘A‘)*26*10
		+(name[2]-‘A‘)*10+name[3]-‘0‘;
		if(stu[base].size()>0)
		{
			sort(stu[base].begin(),stu[base].end());
			printf("%s %d",name,(int)stu[base].size());
			for(j=0;j<stu[base].size();j++)
				printf(" %d",stu[base][j]);
			printf("\n");
		}
		else
			printf("%s 0\n",name);
	}

	return 0;
}


Pat(Advanced Level)Practice--1039(Course List for Student)

上一篇:iOS 7:在一般的 ViewController 中使用static cell


下一篇:育灵童古典歌曲