• PAT 1022 Digital Library


    1022 Digital Library

    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.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

    • Line #1: the 7-digit ID number;
    • Line #2: the book title -- a string of no more than 80 characters;
    • Line #3: the author -- a string of no more than 80 characters;
    • 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;
    • Line #5: the publisher -- a string of no more than 80 characters;
    • Line #6: the published year -- a 4-digit number which is in the range [1000, 3000].

    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.

    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:

    • 1: a book title
    • 2: name of an author
    • 3: a key word
    • 4: name of a publisher
    • 5: a 4-digit number representing the year

    Output Specification:

    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.

    Sample Input:

    1. 3
    2. 1111111
    3. The Testing Book
    4. Yue Chen
    5. test code debug sort keywords
    6. ZUCS Print
    7. 2011
    8. 3333333
    9. Another Testing Book
    10. Yue Chen
    11. test code sort keywords
    12. ZUCS Print2
    13. 2012
    14. 2222222
    15. The Testing Book
    16. CYLL
    17. keywords debug book
    18. ZUCS Print2
    19. 2011
    20. 6
    21. 1: The Testing Book
    22. 2: Yue Chen
    23. 3: keywords
    24. 4: ZUCS Print
    25. 5: 2011
    26. 3: blablabla

    Sample Output:

    1. 1: The Testing Book
    2. 1111111
    3. 2222222
    4. 2: Yue Chen
    5. 1111111
    6. 3333333
    7. 3: keywords
    8. 1111111
    9. 2222222
    10. 3333333
    11. 4: ZUCS Print
    12. 1111111
    13. 5: 2011
    14. 1111111
    15. 2222222
    16. 3: blablabla
    17. Not Found

     总结:用结构体只是写出了题目样例,还是有待进步!

    有几个需要注意的点:

    cin输入后在缓冲区的还存留着回车,如果此时是使用了getline(cin,s),会读取一个回车,导致结果不正确,需要使用一个getchar(),读取回车

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. struct Books{
    6. int id;
    7. string name,author;
    8. mapint> m;
    9. string publisher,year;
    10. };
    11. int main(){
    12. int n,k,t=0;
    13. cin >> n;
    14. Books book[n+1];
    15. for(int i=0;i
    16. cin >> book[i].id;
    17. getchar();
    18. getline(cin,book[i].name);
    19. getline(cin,book[i].author);
    20. char c;
    21. string s;
    22. while(1){
    23. cin >> s;
    24. book[i].m[s]=1;
    25. scanf("%c",&c);
    26. if(c=='\n') break;
    27. }
    28. getline(cin,book[i].publisher);
    29. getline(cin,book[i].year);
    30. }
    31. scanf("%d",&k);
    32. getchar();
    33. string s,w;
    34. for(int i=0;i
    35. getline(cin,s);
    36. t=s[0]-'0';
    37. set<int> v;
    38. if(t>=1 && t<=5) cout << s << endl;
    39. w=s.substr(3);
    40. if(t==1){
    41. for(int j=0;j
    42. if(book[j].name==w){
    43. cout << book[j].id << endl;
    44. }
    45. }
    46. }
    47. else if(t==3){
    48. for(int j=0;j
    49. if(book[j].m[w]) v.insert(book[j].id);
    50. }
    51. if(!v.size()) printf("Not Found\n");
    52. else for(auto it=v.begin();it!=v.end();it++) printf("%d\n",*it);
    53. }
    54. else if(t==2){
    55. for(int j=0;j
    56. if(book[j].author==w) cout << book[j].id << endl;
    57. }
    58. }
    59. else if(t==4){
    60. for(int j=0;j
    61. if(book[j].publisher==w) cout << book[j].id << endl;
    62. }
    63. }
    64. else if(t==5){
    65. for(int j=0;j
    66. if(book[j].year==w) cout << book[j].id << endl;
    67. }
    68. }
    69. }
    70. return 0;
    71. }

    还是看看到老的代码:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. mapint> > title, author, key, pub, year;
    6. void query(mapint> > &m, string &str) {
    7. if(m.find(str) != m.end()) {
    8. for(auto it = m[str].begin(); it != m[str].end(); it++)
    9. printf("%07d\n", *it);
    10. } else
    11. cout << "Not Found\n";
    12. }
    13. int main() {
    14. int n, m, id, num;
    15. scanf("%d", &n);
    16. string ttitle, tauthor, tkey, tpub, tyear;
    17. for(int i = 0; i < n; i++) {
    18. scanf("%d\n", &id);
    19. getline(cin, ttitle);
    20. title[ttitle].insert(id);
    21. getline(cin, tauthor);
    22. author[tauthor].insert(id);
    23. while(cin >> tkey) {
    24. key[tkey].insert(id);
    25. char c = getchar();
    26. if(c == '\n') break;
    27. }
    28. getline(cin, tpub);
    29. pub[tpub].insert(id);
    30. getline(cin, tyear);
    31. year[tyear].insert(id);
    32. }
    33. scanf("%d", &m);
    34. for(int i = 0; i < m; i++) {
    35. scanf("%d: ", &num);
    36. string temp;
    37. getline(cin, temp);
    38. cout << num << ": " << temp << "\n";
    39. if(num == 1) query(title, temp);
    40. else if(num == 2) query(author, temp);
    41. else if(num == 3) query(key, temp);
    42. else if(num == 4) query(pub,temp);
    43. else if(num ==5) query(year, temp);
    44. }
    45. return 0;
    46. }

     还是看看大佬的代码:

    果然,大佬就是大佬,思路简单易懂

    基本思路:将同一类的字符串的id都存在同一个string类型的map中,最后查找看是该字符串是否存在,存在则打印,不存在就打印Not Found

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. mapint> > title, author, key, pub, year;
    6. //此时用map> 这样的变量,可以再同一个字符串存储多个id,也就是只要有这个字符串的id都是可以存进去的
    7. void query(mapint> > &m, string &str) {
    8. if(m.find(str) != m.end()) {
    9. for(auto it = m[str].begin(); it != m[str].end(); it++)
    10. printf("%07d\n", *it);
    11. } else
    12. cout << "Not Found\n";
    13. }
    14. int main() {
    15. int n, m, id, num;
    16. scanf("%d", &n);
    17. string ttitle, tauthor, tkey, tpub, tyear;
    18. for(int i = 0; i < n; i++) {
    19. scanf("%d\n", &id);
    20. getline(cin, ttitle);
    21. title[ttitle].insert(id);//注意这里的insert插入进去的是set,而不是map
    22. getline(cin, tauthor);
    23. author[tauthor].insert(id);
    24. while(cin >> tkey) {
    25. key[tkey].insert(id);
    26. char c = getchar();
    27. if(c == '\n') break;
    28. }
    29. getline(cin, tpub);
    30. pub[tpub].insert(id);
    31. getline(cin, tyear);
    32. year[tyear].insert(id);
    33. }
    34. scanf("%d", &m);
    35. for(int i = 0; i < m; i++) {
    36. scanf("%d: ", &num);
    37. string temp;
    38. getline(cin, temp);
    39. cout << num << ": " << temp << "\n";
    40. if(num == 1) query(title, temp);
    41. else if(num == 2) query(author, temp);
    42. else if(num == 3) query(key, temp);
    43. else if(num == 4) query(pub,temp);
    44. else if(num ==5) query(year, temp);
    45. }
    46. return 0;
    47. }

    好好学习,天天向上!

    我要考研

  • 相关阅读:
    vue3组件封装系列-表格及分页
    LeetCode 290. 单词规律
    MySQL密码不要用0开头!!!
    LAXCUS分布式操作系统是什么?
    轻松上手Jackjson(珍藏版)
    国庆放假作业5
    gRpc_go_dart-1.编写第一个服务
    C语言之for while语句详解
    高效!启科量子线路模拟器 QuSprout 与 Amazon HPC 集成,赋能量子计算
    LSM Tree
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/126823286