PAT 1039 Course List for Student

個人學習記錄,代碼難免不盡人意。
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 (≤40,000), the number of students who look for their course lists, and K (≤2,500), 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 istudent 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

一開始我的代碼是這樣的:

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
using namespace std;int main(){int n,k;scanf("%d %d",&n,&k);set<string> course[k];for(int i=0;i<k;i++){int index,num;scanf("%d %d",&index,&num);for(int j=0;j<num;j++){string name;cin>>name;course[index-1].insert(name);}}string names[n];for(int i=0;i<n;i++){string name;cin >> name;names[i]=name;}for(int i=0;i<n;i++){vector<int> v;for(int j=0;j<k;j++){set<string>::iterator it=course[j].find(names[i]);if(it!=course[j].end()){v.push_back(j+1);}}cout << names[i]<< " " << v.size();for(int j=0;j<v.size();j++)printf(" %d",v[j]);printf("\n");}return 0;
}   

這樣做的話最后一個測試點會超時,看了答案之后發現是不能用string和cin、cout,因此得采用將學生的姓名轉換成數字的形式處理,也就是字符串hash。

#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;const int M=26*26*26*10+1;
vector<int> ans[M];
int getID(char name[]){int id=0;for(int i=0;i<3;i++){id=id*26+(name[i]-'A');}id=id*10+(name[3]-'0');return id;
}int main(void){int n,k;while(scanf("%d%d",&n,&k)!=EOF){for(int i=1;i<=k;i++){int course,ni;scanf("%d%d",&course,&ni);for(int j=1;j<=ni;j++){char name[4];int id;scanf("%s",name);id = getID(name);ans[id].push_back(course);}}for(int i=0;i<n;i++){char name[4];scanf("%s",name);int id=getID(name);sort(ans[id].begin(),ans[id].end());printf("%s %d",name,(int)ans[id].size());for(int j=0;j<ans[id].size();j++){printf(" %d",ans[id][j]);}printf("\n");}}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/36517.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/36517.shtml
英文地址,請注明出處:http://en.pswp.cn/news/36517.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

netty學習分享 二

操作系統IO模型與實現原理 阻塞IO 模型 應用程序調用一個IO函數&#xff0c;導致應用程序阻塞&#xff0c;等待數據準備好。如果數據沒有準備好&#xff0c;一直等待….數據準備好了&#xff0c;從內核拷貝到用戶空間,IO函數返回成功指示。 當調用recv()函數時&#xff0c;系…

釉面陶瓷器皿SOR/2016-175標準上架亞馬遜加拿大站

親愛的釉面陶瓷器皿和玻璃器皿制造商和賣家&#xff0c;亞馬遜加拿大站將執行SOR/2016-175法規。這是一份新的法規&#xff0c;規定了含有鉛和鎘的釉面陶瓷器和玻璃器皿需要滿足的要求。讓我們一起來看一看&#xff0c;為什么要實行SOR/2016-175法規&#xff1f;這是一個保護消…

yolo源碼注釋3——模型配置文件

代碼基于yolov5 v6.0 目錄&#xff1a; yolo源碼注釋1——文件結構yolo源碼注釋2——數據集配置文件yolo源碼注釋3——模型配置文件yolo源碼注釋4——yolo-py 模型配置文件一般放在 models 文件夾下的 XXX.yaml 文件中&#xff0c;以 yolov5s.yaml 為例&#xff1a; # YOLOv…

使用SpringAop切面編程通過Spel表達式實現Controller權限控制

目錄 參考一、概念SpEL表達式 二、開發引入包定義注解定義切面定義用戶上下文 三、測試新建Service在方法上注解新建Service在類上注解運行 參考 SpringBoot&#xff1a;SpEL讓復雜權限控制變得很簡單 一、概念 對于在Springboot中&#xff0c;利用自定義注解切面來實現接口…

opencv實戰項目 手勢識別-手勢音量控制(opencv)

本項目是使用了谷歌開源的框架mediapipe&#xff0c;里面有非常多的模型提供給我們使用&#xff0c;例如面部檢測&#xff0c;身體檢測&#xff0c;手部檢測等。 手勢識別系列文章 1.opencv實現手部追蹤&#xff08;定位手部關鍵點&#xff09; 2.opencv實戰項目 實現手勢跟蹤…

8月14日,每日信息差

1、FF正式交付首輛FF 91 2.0 Futurist Alliance給塔尖用戶 2、消息稱iPhone SE 4設計基于iPhone 14&#xff0c;但仍是后置單攝像頭 3、阿聯酋力推電動汽車發展。該政策將作為一個監管框架&#xff0c;明確電動汽車充電站等基礎設施建設的標準&#xff0c;并推動全國標準統一…

Jay17 2023.8.12日報

8.12 今天做了2題&#xff0c;CTFshow 紅包挑戰8&#xff08;PHP create_function()&#xff09;和BUU [RoarCTF 2019]Easy Java&#xff08;web.xml泄露&#xff09;。 此外一直在打NepCTF&#xff0c;出了一題&#xff08;ez_java_checkin&#xff09;簡單了解了java中shri…

Kafka消息隊列學習(一)

文章目錄 概述核心概念生產者示例同步 / 異步發送消息生產者參數配置ack-確認機制retries - 重試次數compression_type - 消息壓縮類型 分區機制分區策略 消費者消息有序性提交和偏移量偏移量提交方式手動提交 高可用設計 SpringBoot集成Kafka基本使用傳遞對象消息 概述 核心概…

HTTP之cookie基礎學習

目錄 Cookie 什么是Cookie Cookie分類 Cookie版本 Cookie工作原理 Cookie詳解 創建cookie cookie編碼 cookie過期時間選項 Cookie流程 Cookie使用 會話管理 個性化信息 記錄用戶的行為 Cookie屬性 domain選項 path選項 secure選項 cookie…

帶著問題學習分布式系統

寫在前面 聽過很多道理&#xff0c;卻依然過不好這一生。 看過很多關于學習的技巧、方法&#xff0c;卻沒應用到自己的學習中。 隨著年紀變大&#xff0c;記憶力越來越差&#xff0c;整塊的時間也越來越少&#xff0c;于是&#xff0c;越來越希望能夠更高效的學習。學習是一種習…

香港大學余濤組推出開源XLANG Agent!支持三種Agent模式

作者 |小戲、ZenMoore 一個新的未來又逐漸開始從理論走向現實走到我們身邊了。 語言的意義在于使用&#xff0c;而從 ChatGPT 以來這些大規模語言模型的意義&#xff0c;也必然絕不止于 Chat&#xff0c;在四個月前&#xff0c;我們介紹了清華大學關于工具學習的綜述《清華發布…

Python-OpenCV中的圖像處理-圖像特征

Python-OpenCV中的圖像處理-圖像特征 圖像特征Harris角點檢測亞像素級精度的角點檢測Shi-Tomasi角點檢測SIFT(Scale-Invariant Feature Transfrom)SURF(Speeded-Up Robust Features) 圖像特征 特征理解特征檢測特征描述 Harris角點檢測 cv2.cornerHarris(img, blockSize, ks…

海格里斯HEGERLS四向穿梭車倉儲解決方案在電子商務行業中的應用

隨著現代物流&#xff0c;尤其是智能化物流的飛速發展&#xff0c;河北沃克金屬制品有限公司看到了智能物流領域背后的巨大價值和市場空間&#xff0c;深知物流與供應鏈對企業發展的重要性。于是&#xff0c;引進了先進的高科技智能技術—HEGERLS四向穿梭車技術&#xff0c;并迅…

【日常積累】Linux下文件亂碼解決

linux下刪除亂碼文件、目錄 由于編碼原因&#xff0c;在linux服務器上上傳、創建中文文件或目錄時&#xff0c;會產生亂碼&#xff0c;如果想刪除它&#xff0c;有時候發現用rm命令是刪除不了的 這種情況下&#xff0c;用find命令可以刪除亂碼的文件或目錄。 首先進入亂碼文件…

docker 網絡訪問診斷

本地docker開啟nginx服務等&#xff0c; 發現linux系統重啟之后&#xff0c;無法訪問&#xff0c; 進入容器內部&#xff0c;發現可以訪問 但是容器外部&#xff0c;映射端口無法訪問&#xff1b; 診斷之前&#xff0c;發現docker0沒有IP綁定 rootbook:/etc/docker# ip addr …

自制手寫機器人

寫字機器人模擬在畫圖板上寫字效果 寫了一套寫字機器人代碼&#xff0c;有多種字體可供選擇&#xff0c;需要的朋友私信獲取代碼和軟件

Spring5學習筆記— 工廠高級特性

?作者簡介&#xff1a;大家好&#xff0c;我是Leo&#xff0c;熱愛Java后端開發者&#xff0c;一個想要與大家共同進步的男人&#x1f609;&#x1f609; &#x1f34e;個人主頁&#xff1a;Leo的博客 &#x1f49e;當前專欄&#xff1a; Spring專欄 ?特色專欄&#xff1a; M…

創建型模式-原型模式

文章目錄 一、原型模式1. 概述2. 結構3. 實現4. 案例1.5 使用場景1.6 擴展&#xff08;深克隆&#xff09; 一、原型模式 1. 概述 用一個已經創建的實例作為原型&#xff0c;通過復制該原型對象來創建一個和原型對象相同的新對象。 2. 結構 原型模式包含如下角色&#xff1a; …

微服務架構和分布式架構的區別

微服務架構和分布式架構的區別 有&#xff1a;1、含義不同&#xff1b;2、概念層面不同&#xff1b;3、解決問題不同&#xff1b;4、部署方式不同&#xff1b;5、耦合度不同。其中&#xff0c;含義不同指微服務架構是一種將一個單一應用程序開發為一組小型服務的方法&#xff…

使用windows搭建WebDAV服務,并內網穿透公網訪問【無公網IP】

文章目錄 1. 安裝IIS必要WebDav組件2. 客戶端測試3. 使用cpolar內網穿透&#xff0c;將WebDav服務暴露在公網3.1 打開Web-UI管理界面3.2 創建隧道3.3 查看在線隧道列表3.4 瀏覽器訪問測試 4. 安裝Raidrive客戶端4.1 連接WebDav服務器4.2 連接成功4.2 連接成功 1. Linux(centos8…