1039. Course List for Student (25)

来源:互联网 发布:python while循环列表 编辑:程序博客网 时间:2024/05/29 15:04

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 54 7BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE11 4ANN0 BOB5 JAY9 LOR62 7ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR63 1BOB55 9AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

Sample Output:

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

 


 解题思路:很容易想到倒排序的方法,想到用map实现,搞了好久最后一个测试点总是通不过只有23分;看能AC的思路 看第二部分!!

23分代码:

#include <iostream>#include <map>#include <string>#include <cstring>#include <stdio.h>using namespace std;struct Node{    int courseNum;    int courseId[2501];    int index;};string arr[2501][201];    //arr的行号代表课程号int N,K;int main(){    cin>>N>>K;    for(int i=0;i<K;i++){        int mark,n;        int flag = 0;        cin>>mark>>n;        for(int j=0;j<n;j++){            cin>>arr[mark][flag++];        }    }    map<string,Node> myMap;    string needCk[N];    Node *im = new Node[N];    for(int i=0;i<N;i++){        im[i].courseNum = 0;        im[i].index = 0;    }    for(int i=0;i<N;i++){        cin>>needCk[i];        myMap.insert(make_pair(needCk[i],im[i]));    }    for(int i=1;i<2501;i++){        string temp;        int flag = 0;        while(arr[i][flag]!=""){            myMap[arr[i][flag]].courseNum++;            myMap[arr[i][flag]].courseId[myMap[arr[i][flag]].index++] = i;            flag++;        }    }    for(int i=0;i<N;i++){        printf("%s %d",needCk[i].c_str(),myMap[needCk[i]].courseNum);        for(int j=0;j<myMap[needCk[i]].index;j++){            printf(" %d",myMap[needCk[i]].courseId[j]);        }        printf("\n");    }    return 0;}


想了一下午结果还是不能AC,最后参考网上那个的代码,主要是string类型处理比较费时间,转化成int就ok而A~Z是26个字符,很容易想到26进制,后来发现内存超限......最后把个位数改成十进制的  能AC 发现这道题卡时间卡的好厉害!!!

AC的参考代码:

#include<iostream>#include<vector>#include<stdio.h>using namespace std;vector<int> mapping[26*26*26*10];int main(){int N,K;scanf("%d%d",&N,&K);vector<vector<char*> > course(K+1);for(int i = 0; i < K; i++){int cid, n;scanf("%d%d",&cid,&n);course[cid].resize(n);for(int j = 0; j < n; ++j){char* name = new char[4];scanf("%s",name);course[cid][j] = name;}}//structure the mappingfor(int i = 1; i <= K; i++){for(int j = 0; j < course[i].size(); j++){char* name = course[i][j];int index = (name[0]-'A')*26*26*10+(name[1]-'A')*26*10+(name[2]-'A')*10+(name[3]-'0');mapping[index].push_back(i);}}for(int i = 0; i < N; ++i){char name[4];scanf("%s",name);printf("%s",name);int index = (name[0]-'A')*26*26*10+(name[1]-'A')*26*10+(name[2]-'A')*10+(name[3]-'0');printf(" %d", mapping[index].size());for(int j = 0; j < mapping[index].size(); j++){printf(" %d", mapping[index][j]);}printf("\n");}return 0;}


 

0 0
原创粉丝点击