problem

  1. A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID's.
  2. Input Specification:
  3. Each input file contains one test case. For each case, the first line contains a positive integer N (<=10000) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:
  4. Line #1: the 7-digit ID number;
  5. Line #2: the book title -- a string of no more than 80 characters;
  6. Line #3: the author -- a string of no more than 80 characters;
  7. Line #4: the key words -- each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
  8. Line #5: the publisher -- a string of no more than 80 characters;
  9. Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].
  10. It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.
  11. After the book information, there is a line containing a positive integer M (<=1000) which is the number of user's search queries. Then M lines follow, each in one of the formats shown below:
  12. 1: a book title
  13. 2: name of an author
  14. 3: a key word
  15. 4: name of a publisher
  16. 5: a 4-digit number representing the year
  17. Output Specification:
  18. For each query, first print the original query in a line, then output the resulting book ID's in increasing order, each occupying a line. If no book is found, print "Not Found" instead.
  19. Sample Input:
  20. 3
  21. 1111111
  22. The Testing Book
  23. Yue Chen
  24. test code debug sort keywords
  25. ZUCS Print
  26. 2011
  27. 3333333
  28. Another Testing Book
  29. Yue Chen
  30. test code sort keywords
  31. ZUCS Print2
  32. 2012
  33. 2222222
  34. The Testing Book
  35. CYLL
  36. keywords debug book
  37. ZUCS Print2
  38. 2011
  39. 6
  40. 1: The Testing Book
  41. 2: Yue Chen
  42. 3: keywords
  43. 4: ZUCS Print
  44. 5: 2011
  45. 3: blablabla
  46. Sample Output:
  47. 1: The Testing Book
  48. 1111111
  49. 2222222
  50. 2: Yue Chen
  51. 1111111
  52. 3333333
  53. 3: keywords
  54. 1111111
  55. 2222222
  56. 3333333
  57. 4: ZUCS Print
  58. 1111111
  59. 5: 2011
  60. 1111111
  61. 2222222
  62. 3: blablabla
  63. Not Found

tip

  • 考察map的使用。

answer

  1. #include<iostream>
  2. #include<string>
  3. #include<map>
  4. #include<set>
  5. #include<vector>
  6. using namespace std;
  7. int N, M;
  8. map<string, set<int> > titleM, authorM, keyM, pubM, yearM;
  9. vector<string> Explode(string s){
  10. string buff = "";
  11. vector<string> v;
  12. for(int i = 0; i < s.size(); i++){
  13. if(s[i] != ' ') buff += s[i];
  14. if(s[i] == ' ' && buff != ""){
  15. v.push_back(buff);
  16. buff = "";
  17. }
  18. }
  19. if(buff != "") v.push_back(buff);
  20. return v;
  21. }
  22. void Query(string &q, map<string, set<int> > &m){
  23. set<int>::iterator itS;
  24. if(m.find(q) != m.end())
  25. for(itS = m[q].begin(); itS != m[q].end(); itS++){
  26. printf("%07d\n", *itS);
  27. }
  28. else cout<<"Not Found"<<endl;
  29. }
  30. int main(){
  31. // freopen("test.txt", "r", stdin);
  32. scanf("%d", &N);
  33. for(int i = 0; i < N; i ++){
  34. int id;
  35. string title, author, key, pub, year;
  36. scanf("%d\n", &id);
  37. getline(cin, title);
  38. getline(cin, author);
  39. getline(cin, key);
  40. getline(cin, pub);
  41. getline(cin, year);
  42. titleM[title].insert(id);
  43. authorM[author].insert(id);
  44. pubM[pub].insert(id);
  45. yearM[year].insert(id);
  46. vector<string> keys = Explode(key);
  47. for(int j = 0; j < keys.size(); j++){
  48. keyM[keys[j]].insert(id);
  49. }
  50. }
  51. scanf("%d", &M);
  52. for(int i = 0; i < M; i++){
  53. int num;
  54. scanf("%d: ", &num);
  55. string temp;
  56. getline(cin, temp);
  57. cout<<num<<": "<<temp<<endl;
  58. switch(num){
  59. case 1:{
  60. Query(temp, titleM);
  61. break;
  62. }
  63. case 2:{
  64. Query(temp, authorM);
  65. break;
  66. }
  67. case 3:{
  68. Query(temp, keyM);
  69. break;
  70. }
  71. case 4:{
  72. Query(temp, pubM);
  73. break;
  74. }
  75. case 5:{
  76. Query(temp, yearM);
  77. break;
  78. }
  79. }
  80. }
  81. return 0;
  82. }

exprience

  • 熟练使用getline(), getchar(), scanf() 与printf()

  • 需要特别注意题目中出现的带有位数描述的数字,再输出时需要按位数输出。

1022 Digital Library (30)(30 point(s))的更多相关文章

  1. PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  2. pat 甲级 1022. Digital Library (30)

    1022. Digital Library (30) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Di ...

  3. 1022 Digital Library (30 分)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  4. PAT 1022 Digital Library[map使用]

    1022 Digital Library (30)(30 分) A Digital Library contains millions of books, stored according to th ...

  5. PTA 1004 Counting Leaves (30)(30 分)(dfs或者bfs)

    1004 Counting Leaves (30)(30 分) A family hierarchy is usually presented by a pedigree tree. Your job ...

  6. A1095 Cars on Campus (30)(30 分)

    A1095 Cars on Campus (30)(30 分) Zhejiang University has 6 campuses and a lot of gates. From each gat ...

  7. 1022 Digital Library——PAT甲级真题

    1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...

  8. 1022 Digital Library (30)(30 分)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

  9. 1022. Digital Library (30)

    A Digital Library contains millions of books, stored according to their titles, authors, key words o ...

随机推荐

  1. bzoj 1564 [NOI2009]二叉查找树(树形DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1564 [题意] 给定一个Treap,总代价为深度*距离之和.可以每次以K的代价修改权值 ...

  2. 20155310 2016-2017-2 《Java程序设计》第六周学习总结

    20155310 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 4.1 Y86指令集体系结构 •有8个程序寄存器:%eax.%ecx.%edx.%ebx.% ...

  3. zzd 的割草机(Lawnmower)

    评测传送门 [题目描述] 已知花坛为一个 n * m 的矩形,草只会长在某些个格子上,zzd 有一个割草机,一开始,zzd 站在(1,1)处,面向(1,m)(面向右).每次 zzd 有两个选择(耗费一 ...

  4. 文件操作fstream

    c++文件操作详解 2009-04-16 20:46:35|  分类: C/C++|举报|字号 订阅 C++ 通过以下几个类支持文件的输入输出: ofstream: 写操作(输出)的文件类 (由ost ...

  5. SDL封装的系统操作(转载)

    Andrew Haung bluedrum@163.com SDL封装很多操作系统的功能,为了保证SDL程序可移植性,最好尽量用这一些封装函数,哪果没有的话,才使用各种操作本地函数.  对于如何封各个 ...

  6. Python time()方法

    from:http://www.runoob.com/python/att-time-time.html 描述 Python time time() 返回当前时间的时间戳(1970纪元后经过的浮点秒数 ...

  7. MFC将二进制文件导入资源后释放

    1.前言 前一篇笔记记录了怎么修改PE,此篇记录下如何利用自身的资源文件. 2.编程思路 获得资源句柄 - 获得资源文件大小 - 加载资源文件 - 锁定资源并获得其指针. 3.实践代码 1)编译以下代 ...

  8. MySQL 执行SQL脚本 报ERROR 1231 (42000)的解决办法【转】

    今天在source mysqldump 备份文件时,发现导入的过程中报如下的错误: ERROR 1231 (42000): Variable 'time_zone' can't be set to t ...

  9. LVS ARP广播产生的问题和处理方式【转】

    转自 LVS ARP广播产生的问题和处理方式-htckiller2010-ChinaUnix博客http://blog.chinaunix.net/uid-24960107-id-193084.htm ...

  10. mybatis 控制台打印sql脚本

    在mybatis-config.xml文件中加一句 <setting name="logImpl" value="STDOUT_LOGGING" /> ...