PAT

xiaoxiao2021-02-28  49

// // main.cpp // PAT_1039. Course List for Student // // Created by wjq on 17/5/7. // Copyright © 2017年 wjq. All rights reserved. // #include <iostream> #include <algorithm> #include <map> #include <vector> using namespace std; vector<int> v; map <int,vector<int> > m; int numOfStudents,numOfLessons; int LessonId,numOfLessonStudents; int name; char temp[100]; int hashname(char *name) { return (name[0]-'A')*26*26*10+(name[1]-'A')*26*10+(name[2]-'A')*10+(name[3] - '0'); } int main(int argc, const char * argv[]) { scanf("%d%d",&numOfStudents,&numOfLessons); for(int i=0;i<numOfLessons;i++) { scanf("%d%d",&LessonId,&numOfLessonStudents); for(int j=0;j<numOfLessonStudents;j++) { scanf("%s",temp); name=hashname(temp); m[name].push_back(LessonId); } } for(int i=0;i<numOfStudents;i++) { scanf("%s",temp); name=hashname(temp); sort(m[name].begin(),m[name].end()); cout<<temp<<" "<<m[name].size(); for(int j=0;j<m[name].size();j++) cout<<" "<<m[name][j]; cout<<endl; } return 0; }

自己写用的mapran然后最后一个case没过,参考了一位博主的代码,发现原来题目中的名字格式是可以hash的!!!这样就不需要字符串的很多操作了..

以后遇到与标准格式的字符串处理的东西记得hash成数字!

转载请注明原文地址: https://www.6miu.com/read-84982.html

最新回复(0)