題目描述
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 N
i (<= 200) are given in a line. Then in the next line, N
i 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.
題意
假設全校有最多40000名學生和最多2500門課程。現給出每門課的選課學生名單,要求輸出每一個前來查詢的學生的選課清單。git
輸入格式:
輸入的第一行是兩個正整數:N(≤40000),爲前來查詢課表的學生總數;K(≤2500),爲總課程數。此後順序給出課程1到K的選課學生名單。格式爲:對每一門課,首先在一行中輸出課程編號(簡單起見,課程從1到K編號)和選課學生總數(之間用空格分隔),以後在第二行給出學生名單,相鄰兩個學生名字用1個空格分隔。學生姓名由3個大寫英文字母+1位數字組成。選課信息以後,在一行內給出了N個前來查詢課表的學生的名字,相鄰兩個學生名字用1個空格分隔。api
輸出格式:
對每位前來查詢課表的學生,首先輸出其名字,隨後在同一行中輸出一個正整數C,表明該生所選的課程門數,隨後按遞增順序輸出C個課程的編號。相鄰數據用1個空格分隔,注意行末不能輸出多餘空格。數組
樣例輸入
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
用到了映射,vector,sortide
小技巧就是在名字字符串相對固定的狀況下,咱們能夠把一個整數值映射。也方便作後面的排序(其int的大小排序恰好對應於名字大小的排序)spa
第一次用課程序號做爲數組下標,超時了code
1 #include <stdio.h> 2 #include <vector> 3 using namespace std; 4 5 int hashFunc(char S[],int len) 6 { 7 int id=0; 8 for(int i=0;i<len-1;i++) 9 { 10 id=id*26+(S[i]-'A'); 11 } 12 id=id*10+S[len-1]-'0'; 13 return id; 14 } 15 16 17 int main() 18 { 19 int n,k; 20 int t,i,g; 21 vector<int> vi[40001]; 22 char str[5]; 23 scanf("%d %d",&n,&k); 24 for(i=0;i<k;i++) 25 { 26 int a,b; 27 scanf("%d %d",&a,&b); 28 while(b--) 29 { 30 scanf("%s",str); 31 t=hashFunc(str,4); 32 vi[a].push_back(t); 33 } 34 } 35 while(n--) 36 { 37 scanf("%s",str); 38 printf("%s",str); 39 int num=0; 40 t=hashFunc(str,4); 41 vector<int> vt; 42 for(i=1;i<=k;i++) 43 { 44 for(g=0;g!=vi[i].size();g++) 45 { 46 if(t==vi[i][g]) 47 { 48 num++; 49 vt.push_back(i); 50 break; 51 } 52 } 53 } 54 if(num) 55 { 56 printf(" %d",num); 57 for(i=0;i!=vt.size();i++) 58 { 59 printf(" %d",vt[i]); 60 } 61 } 62 else printf(" 0"); 63 printf("\n"); 64 } 65 return 0; 66 }
後來用人名的映射做爲數組下標,用空間來代替時間orm
1 #include <stdio.h> 2 #include <algorithm> 3 #include <vector> 4 using namespace std; 5 6 const int maxx = 26 * 26 * 26 * 10 + 1; 7 vector<int> vi[maxx]; 8 9 int hashFunc(char S[],int len) 10 { 11 int id=0; 12 for(int i=0;i<len-1;i++) 13 { 14 id=id*26+(S[i]-'A'); 15 } 16 id=id*10+S[len-1]-'0'; 17 return id; 18 } 19 20 21 int main() 22 { 23 int n,k; 24 int t,i; 25 char str[5]; 26 scanf("%d %d",&n,&k); 27 for(i=0;i<k;i++) 28 { 29 int a,b; 30 scanf("%d %d",&a,&b); 31 while(b--) 32 { 33 scanf("%s",str); 34 t=hashFunc(str,4); 35 vi[t].push_back(a); 36 } 37 } 38 while(n--) 39 { 40 scanf("%s",str); 41 printf("%s",str); 42 t=hashFunc(str,4); 43 sort(vi[t].begin(), vi[t].end()); 44 printf(" %d",vi[t].size()); 45 for(i=0;i!=vi[t].size();i++) 46 { 47 printf(" %d",vi[t][i]); 48 } 49 printf("\n"); 50 } 51 return 0; 52 }
對比上一種blog
1 #include <stdio.h> 2 #include <set> 3 #include <map> 4 #include <algorithm> 5 6 using namespace std; 7 8 int hashfun(char str[],int n) 9 { 10 int i; 11 int ans=0; 12 for(i=0;i<n-1;i++) 13 { 14 ans=ans*26+str[i]-'A'; 15 } 16 ans=ans*10+str[n-1]; 17 return ans; 18 } 19 20 int main() 21 { 22 int n,k; 23 char str[5]; 24 map<int,set<int> >mp; 25 set<int>::iterator it; 26 scanf("%d %d",&n,&k); 27 for(int i=0;i<k;i++) 28 { 29 int a,b; 30 scanf("%d %d",&a,&b); 31 int t; 32 while(b--) 33 { 34 scanf("%s",str); 35 t=hashfun(str,4); 36 mp[t].insert(a); 37 } 38 } 39 while(n--) 40 { 41 scanf("%s",str); 42 printf("%s",str); 43 int t; 44 t=hashfun(str,4); 45 printf(" %d",mp[t].size()); 46 for(it=mp[t].begin();it!=mp[t].end();it++) 47 { 48 printf(" %d",*it); 49 } 50 printf("\n"); 51 } 52 return 0; 53 }