1039. Course List for Student (25)
来源:互联网 发布:天猫和淘宝的营销策略 编辑:程序博客网 时间:2024/06/04 20:15
倒排索引题
这道题目好像是为数不多的一道卡时题,你在PAT刷的很happy的时候,还是要注意下string的滥用的。
因为string的拷贝以及比较操作都要比char数组相同操作的效率低。
另外其实vector的频繁改变数组大小其实也比较耗时,不过在PAT上还是都可以用vector的。
#include<iostream>#include<vector>#include<set>#include<map>#include<queue>#include<algorithm>#include<string>#include<string.h>using namespace std;vector<int> name2course[26*26*26*10];int main(){//inputint n, k;scanf("%d%d",&n,&k);//if use string instead of char*, then will be TLE, the copy time is also a bottle neck//may be if we do not use vector container and just use array, may be string can be used//in such a TLE case, every where we use STL container or cin/cout it can be a bottle neckvector<vector<char*>> course(k+1);for(int i = 0; i < k; ++i){int cid, num;scanf("%d%d",&cid,&num);course[cid].resize(num);for(int j = 0; j < num; ++j){char* name = new char[4];scanf("%s",name);course[cid][j] = name;}}//build the mapfor(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');name2course[index].push_back(i);}}//for queryfor(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", name2course[index].size());for(int j = 0; j < name2course[index].size(); ++j){printf(" %d", name2course[index][j]);}printf("\n");}//releasefor(int i = 0; i < course.size(); ++i){for(int j = 0; j < course[i].size(); ++j){delete[] course[i][j];}}return 0;}
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)-PAT
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- PAT 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- PAT 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- PAT 1039. Course List for Student (25)
- 1039. Course List for Student (25)
- PAT 1039. Course List for Student (25)
- android.uid.system下解决sdcard读写问题
- 蒙层效果
- 做设计得收藏的100个网址 对你绝对有用的好资源!
- zju1002-Fire Net
- 如何使用Junit测试使用Spring框架的代码
- 1039. Course List for Student (25)
- Spring注解@Component、@Repository、@Service、@Controller区别
- wpf中多窗口线程处理
- Oracle 10g 正则表达式
- POJ2226 Muddy Fields
- 用JAVA实现串宫压运(串宫大法)排盘
- ajax提交form表单
- hibernate缓存:一级缓存和二级缓存
- 张老师交通灯