【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)

题意:

输入两个正整数N和K(N<=40000,K<=2500),分别为学生和课程的数量。接下来输入K门课的信息,先输入每门课的ID再输入有多少学生选了这门课,接下来输入学生们的ID。最后N次询问,输入学生的ID输出该学生选了多少们课,输出所选课程的数量,按照递增序输出课程的ID。

trick:

第5个数据点出现段错误,把原本以map存学生对应ID再映射vector存储该学生所选课程改成vector嵌套在map内,就没有段错误的问题出现,疑似映射过程中指针漂移???

代码:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
unordered_map<string,vector<int> >name;
string s;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin>>n>>k;
int x,num;
int cnt=0;
for(int i=1;i<=k;++i){
cin>>x>>num;
for(int j=1;j<=num;++j){
cin>>s;
if(name.find(s)==name.end())
name[s]=vector<int>{x};
else
name[s].push_back(x);
}
}
string ss;
for(int i=1;i<=n;++i){
cin>>ss;
auto&idd=name[ss];
cout<<ss<<" "<<idd.size();
sort(idd.begin(),idd.end());
for(auto it:idd)
cout<<" "<<it;
cout<<"\n";
}
return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/11588438.html

时间: 2024-08-05 08:19:50

【PAT甲级】1039 Course List for Student (25 分)(vector嵌套于map,段错误原因未知)的相关文章

PAT Advanced 1039 Course List for Student (25分) (STL)

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

PAT 甲级 1066 Root of AVL Tree (25 分)(快速掌握平衡二叉树的旋转,内含代码和注解)***

1066 Root of AVL Tree (25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this

【PAT甲级】1052 Linked List Sorting (25分)

1052 Linked List Sorting (25分) A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, yo

PAT:1039. Course List for Student (25) 部分正确(最后一个大数据错误)

#include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; const int MAX=40010; const int H=26*26*26*10+10; int n,k; //n个人,k门课 vector<int> HAR[H]; int getID(char str[]) //获取名字对应的哈希ID { int

【PAT甲级】1110 Complete Binary Tree (25分)

题意: 输入一个正整数N(<=20),代表结点个数(0~N-1),接着输入N行每行包括每个结点的左右子结点,'-'表示无该子结点,输出是否是一颗完全二叉树,是的话输出最后一个子结点否则输出根节点. trick: 用char输入子结点没有考虑两位数的结点??... stoi(x)可以将x转化为十进制整数 AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace

【PAT甲级】1044 Shopping in Mars (25 分)(前缀和,双指针)

题意: 输入一个正整数N和M(N<=1e5,M<=1e8),接下来输入N个正整数(<=1e3),按照升序输出"i-j",i~j的和等于M或者是最小的大于M的数段. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int a[100007];int sum[100007];vector<pair<int,int> >ans;int m

【PAT甲级】1052 Linked List Sorting (25 分)

题意: 输入一个正整数N(<=100000),和一个链表的头结点地址.接着输入N行,每行包括一个结点的地址,结点存放的值(-1e5~1e5),指向下一个结点的地址.地址由五位包含前导零的正整数组成.以头结点地址开始的这条链表以值排序后得到的链表的长度和头结点,接着以升序按行输出每个结点的地址和值以及指向下一个结点的地址. trick: 题干说的postive N可是数据点4出现了N==0的数据,有些不解如果N==0应该包含的话不应该用nonnegative N吗... 数据点1包含有些结点并非在

【PAT甲级】1056 Mice and Rice (25 分)

题意: 输入两个正整数N和M(<=1000),接着输入两行,每行N个数,第一行为每只老鼠的重量,第二行为每只老鼠出战的顺序.输出它们的名次.(按照出战顺序每M只老鼠分为一组,剩余不足M只为一组,每组只能有一个胜者,其他老鼠排名均为这一轮胜者数量+1) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int n,m;int a[1007],b[1007];int num;int ans[1

【PAT甲级】1074 Reversing Linked List (25 分)

题意: 输入链表头结点的地址(五位的字符串)和两个正整数N和K(N<=100000,K<=N),接着输入N行数据,每行包括结点的地址,结点的数据和下一个结点的地址.输出每K个结点局部反转的链表. trick: 测试点6包含一些不在起点这条链表上的结点. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;map<string,pair<int,string> >