1. 程式人生 > >CodeUp中關於vector的兩道題目

CodeUp中關於vector的兩道題目


兩道題大同小異 直接寫在一起了
問題 A: Course List for Student (25)
問題 B: Student List for Course (25)
題目連結 http://codeup.cn/contest.php?cid=100000596

問題 A: Course List for Student (25)

時間限制: 1 Sec 記憶體限制: 32 MB
提交: 372 解決: 110
[提交][狀態][討論版][命題人:外部匯入]

題目描述

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.

輸入

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.

輸出

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.

樣例輸入

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

樣例輸出

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

一開始還是對STL理解不夠深,導致想歪了很久,第一題參考了別人的解答,瞭解到以下幾點
1.既然題目中學生的姓名是固定的,那麼我們可以用hash來將學生姓名轉化為int儲存在數組裡面,這樣查詢起來方便多了
2. vector的不同寫法

//這樣寫跟vector<vector<int> > List 功能一樣,用起來沒問題!!只不過性質不一樣
vector<int> List[MaxN];
//std::vector<int> a[10]; //a是一個向量陣列
//std::vector<int> b(10); //b是一個向量
//std::vector<std::vector<int>> c; //c是一個向量 
  1. 自己制定hash規則,只要足夠大避免重複就行,注意上界。
    程式碼使用hash將char[]轉化為int儲存,這樣直接新增課程編號進入不同的學生下就行了,最後按查詢的順序直接輸出即可
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
//ÒòΪѧÉúÐÕÃû¸ø¶¨µÄ£¬¿ÉÒÔÓÃhashÓ³Éäµ½Êý×éÀïÃæ´æ´¢£¡ 
const int MaxN = 180000;  // ('Z' - 'A') * 26 * 26 * 10 + ('Z' - 'A') * 26 * 10 + ('Z' - 'A') * 10 + '9' - '0';
//¶ÔÓ¦×ÅѧÉúÏÂµÄ¿Î³Ì 
//ÕâÑùд¸úvector<vector<int> > List Ò»ÑùµÄ£¡£¡
vector<int> List[MaxN];
//std::vector<int> a[10]; //aÊÇÒ»¸öÏòÁ¿Êý×é
//std::vector<int> b(10); //bÊÇÒ»¸öÏòÁ¿
//std::vector<std::vector<int>> c; //cÊÇÒ»¸öÏòÁ¿ 

//²Î¿¼ https://blog.csdn.net/fantasydreams/article/details/79279876 
int Toint(char a[])
{
	//hash¹æÔò 
	return (a[0] - 'A') * 26 * 26 * 10 + (a[1] - 'A') * 26 * 10 + (a[2] - 'A') * 10 + a[3] - '0';
}
int main()
{
	int find_n,class_n;
	while(scanf("%d%d",&find_n,&class_n)!=EOF)
	{
		for(int i=0;i<class_n;i++)
		{
			int index,stuNum;
			scanf("%d %d",&index,&stuNum);
			for(int i=0;i<stuNum;i++)
			{
				char name[5];
				scanf("%s",name);
				List[Toint(name)].push_back(index);
			}
		}
	
		while(find_n)
		{
			char name[5];
			scanf("%s",name);
			int ind=Toint(name);
			printf("%s %d ",name,List[ind].size());
			sort(List[ind].begin(),List[ind].end());
			for(int i=0;i<List[ind].size();i++)
			{
				if(i!=0) printf(" ");
				printf("%d",List[ind][i]);
			}
			if(find_n) printf("\n");
			find_n--;
			
		}	
	}

} 

問題 B: Student List for Course (25)

時間限制: 1 Sec 記憶體限制: 64 MB
提交: 113 解決: 76
[提交][狀態][討論版][命題人:外部匯入]

題目描述

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.

輸入

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.

輸出

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.

樣例輸入

10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

樣例輸出

1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

讀完題目你就發現,第二題是反過來的第一題。
第一題是給定課程編號和上課學生,再根據學生姓名查他參加的課程
第二題是給定學生和他參與的課程編號,要你推出每個課程下有多少名學生和學生資訊
這道題不用hash,直接用二維向量來儲存課程編號和學生姓名即可

#include<cstdio>
#include<vector>
#include<algorithm>
#include<string>
#include<set>
using namespace std;
int main()
{
	//學生總數 課程總數 
	int stuN,countN;
	while(scanf("%d %d",&stuN,&countN)!=EOF)
	{
		vector<string> list[countN+1];
		for(int i=0;i<stuN;i++)
		{
			char name[5];
//			string name;
			int sum=0;
			scanf("%s %d ",&name,&sum);
			int id=0;
			for(int i=0;i<sum;i++)
			{
				scanf("%d",&id);
				list[id].push_back(name);
			}
		}
		//注意一下 i從1開始,到count結束
		for(int i=1;i<=countN;i++)
		{
			sort(list[i].begin(),list[i].end());
		}
		for(int i=1;i<=countN;i++)
		{
			printf("%d %d\n",i,list[i].size());
			for(int j=0;j<list[i].size();j++)
			{
				printf("%s\n",list[i][j].c_str());
			}
		}
	}
}