A1022 Digital Library

摘要
Title:
Tag: 字符串、getline
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

A1022 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.

  • 思路

    整体思路:就是存取,然后遍历查找,其中keywords可以用map或者set加快查询

    ps:

    • pta的id会有前导0的存在,存的时候尽量用string
    • Switch case时,case中需要break
    • getline的用法
      • 在这介绍两种类似于Python3的split读入方法
      • 首先
        • istringstream类 用于执行C++风格的串流的输入操作
        • ostringstream类 用于执行C++风格的串流的输出操作
        • stringstream类 同时可以支持C++风格的串流的输入输出操作
      • 所以这里可以用 istringstream 或者 stringstream 定义输入流,类似于 stringstream ssin(words);, 意思是定义流ssin,初始化为words对应的字符串
      • 定义完流,就可以从流中获取字符串
        • 第一种方法,也是特殊的一种 while (ssin >> word) 这样默认分隔符为空格,也就是相当于我们的cin,它会一直读完流中的字符串
        • 第二种方法,比较普遍的一种,可以设出特别的分隔符while(getline(ssin, token, '|'))
          • 就是用getline进行处理,getline的第三个参数就是分隔符,默认是\n(换行符),这里可以替换为别的分隔符
          • token就是每次处理得到的字符串
    • 还有就是,cin完如果要getline,需要先getchar
    • puts和ios加速,不能共存
  • 代码

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-01-05 16:08:20
    * @FilePath: \GPLT\A1022\A1022.cpp
    * @LastEditTime: 2023-01-05 19:44:38
    */
    #include <bits/stdc++.h>

    using namespace std;
    #define SZ(X) ((int)(X).size())
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)

    const int N = 1e4 + 10;
    int n, m;
    struct sa
    {
    string id;
    string title;
    string name;
    set<string> words;
    string pub;
    string year;
    };

    int main()
    {
    cin >> n;
    getchar();
    vector<sa> a(n);
    for (int i = 0; i < n; ++i)
    {
    getline(cin, a[i].id);
    getline(cin, a[i].title);
    getline(cin, a[i].name);

    string words, word;
    getline(cin, words);
    stringstream ssin(words);
    while (ssin >> word)
    a[i].words.insert(word);

    getline(cin, a[i].pub);
    getline(cin, a[i].year);
    }

    cin >> m;
    getchar();

    int op;
    string q;
    while (m--)
    {
    vector<string> ans;
    getline(cin, q);
    cout << q << '\n';
    op = q[0] - '0';
    string ques = q.substr(3);

    for (auto &[id, title, name, words, pub, year] : a)
    {
    switch (op)
    {
    case 1:
    if (title == ques)
    ans.push_back(id);
    break;
    case 2:
    if (name == ques)
    ans.push_back(id);
    break;
    case 3:
    if (words.count(ques))
    ans.push_back(id);
    break;
    case 4:
    if (pub == ques)
    ans.push_back(id);
    break;
    case 5:
    if (year == ques)
    ans.push_back(id);
    break;
    default:
    break;
    }
    }
    if (!SZ(ans))
    {
    cout << "Not Found\n";
    continue;
    }
    sort(ans.begin(), ans.end());
    for (auto id : ans)
    cout << id << '\n';
    }

    return 0;
    }
使用搜索:谷歌必应百度