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 (≤10​4​​) 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:

3
1111111
The Testing Book
Yue Chen
test code debug sort keywords
ZUCS Print
2011
3333333
Another Testing Book
Yue Chen
test code sort keywords
ZUCS Print2
2012
2222222
The Testing Book
CYLL
keywords debug book
ZUCS Print2
2011
6
1: The Testing Book
2: Yue Chen
3: keywords
4: ZUCS Print
5: 2011
3: blablabla

Sample Output:

1: The Testing Book
1111111
2222222
2: Yue Chen
1111111
3333333
3: keywords
1111111
2222222
3333333
4: ZUCS Print
1111111
5: 2011
1111111
2222222
3: blablabla
Not Found

我写的暴力丑学版本(由于给的数据不大,所以也能过)

//暴力丑学
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#define MAXN 10001
using namespace std;

class Book
{
public:
    string id;
    string title;
    string author;
    string publisher;
    vector<string> keywords;
    int year;
    bool is_key(string keyword)
    {
        for (int i = 0; i < keywords.size(); i++)
        {
            if (keywords[i] == keyword)
            {
                return true;
            }
        }
        return false;
    }
} books[MAXN];
bool cmp(Book a, Book b)
{
    return a.id.compare(b.id) < 0;
}

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> books[i].id;
        getchar();
        getline(cin, books[i].title);
        getline(cin, books[i].author);
        do
        {
            string k;
            cin >> k;
            books[i].keywords.push_back(k);
        } while (getchar() != '\n');
        getline(cin, books[i].publisher);
        cin >> books[i].year;
    }
    sort(books, books + n, cmp);
    int m;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        int qi;
        cin >> qi;
        getchar();
        getchar();
        string s;
        int num = 0;
        switch (qi)
        {
        case 1:
            getline(cin, s);
            cout << qi << ": " << s << endl;
            for (int i = 0; i < n; i++)
            {
                if (books[i].title == s)
                {
                    cout << books[i].id << endl;
                    num++;
                }
            }

            break;
        case 2:
            getline(cin, s);
            cout << qi << ": " << s << endl;
            for (int i = 0; i < n; i++)
            {
                if (books[i].author == s)
                {
                    cout << books[i].id << endl;
                    num++;
                }
            }
            break;
        case 3:
            cin >> s;
            cout << qi << ": " << s << endl;
            for (int i = 0; i < n; i++)
            {
                if (books[i].is_key(s))
                {
                    cout << books[i].id << endl;
                    num++;
                }
            }
            break;
        case 4:
            getline(cin, s);
            cout << qi << ": " << s << endl;
            for (int i = 0; i < n; i++)
            {
                if (books[i].publisher == s)
                {
                    cout << books[i].id << endl;
                    num++;
                }
            }
            break;
        case 5:
            int y;
            cin >> y;
            cout << qi << ": " << y << endl;
            for (int i = 0; i < n; i++)
            {
                if (books[i].year == y)
                {
                    cout << books[i].id << endl;
                    num++;
                }
            }
            break;
        }
        if (num == 0)
        {
            cout << "Not Found" << endl;
        }
    }
    return 0;
}

在处理字串的时候需要注意的是:对于“读取一行”的函数,如果是返回char*的话用的是gets()函数,而如果是返回string的话应该使用getline()函数(需要#include<string>),写法是getline(cin,s);,会丢弃末尾的换行符(是丢弃,不是读取,也不是读取到就停止)。

还需要注意的一点是cin直接读取字符串是“读取到换行符就停止”,而不是丢弃,换行符仍会留在istream里。因此在cin最后一个属性后直接getline的话会得到一个空串,因此中间需要getchar()吸收掉多余的换行符。

(btw,iostream库中有一个函数叫做cin.getline(s,length,end),表示“从istream中读取长度为length的子串到s中,当接收到字符end时直接停止”,可以接收空格,end缺省值为’\0’)

最后,这种子串处理题需要注意“输出x位序号”、“输出x位地址”,同“精确到x位小数”不一样,前者可以直接使用string来保存属性,使用s.compare()函数来进行排序函数sort的比较cmp。

注意在cmp(astring,bstring)函数里有“return astring.compare(bstring)>0;”,“astring.compare(bstring)>0”表示a的字典序大于b,return正值代表前者(astring)排在后者(bstring)后面,综合表示字典序正序排列。由于为了防止忘记到底是什么意思,最好当场试一下。

对于格式化输出,个人还是推荐printf(比较熟悉,注意long long的输出是”%lld”)。

下面是标准题解的精巧做法(这里的多态实现很精妙,但是一般很难用到,实际需要注意到的是这里map(类似于java的HashMap,提供映射,这样就可以从属性反查询id,算是空间换时间)和set(去重)的使用)。

对set的遍历需要使用set<T>::iterator it = aset.begin() 之类的语句。

对map的插入不需要对键值进行实现声明,直接使用map[key]即可(后面的insert是set的成员方法)。

// 对 STL库 map和set的使用 + 一般的字符串处理函数 + 精妙的多态
#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <set>
using namespace std;
map<string, set<int>> titles, authors, keywords, publishers, years;
void query(map<stringset<int>> &mp, string &s)
{
    if (mp.find(s) == mp.end())
        cout << "Not Found" << endl;
    else
    {
        for (set<int>::iterator i = mp[s].begin(); i != mp[s].end(); i++)
        { // 注意这里set迭代器的使用!!!
            printf("%07d\n", *i);
        }
    }
}
int main()
{
    int n;
    cin >> n;
    getchar();
    for (int i = 0; i < n; i++)
    {
        int id;
        cin >> id;
        getchar();
        string title, author, keyword, publisher, year;
        getline(cin, title);
        titles[title].insert(id);
        getline(cin, author);
        authors[author].insert(id);
        do
        {
            cin >> keyword;
            keywords[keyword].insert(id);
        } while (getchar() != '\n');
        getline(cin, publisher);
        publishers[publisher].insert(id);
        getline(cin, year);
        years[year].insert(id);
    }
    int m;
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        int qi;
        cin >> qi;
        getchar();
        getchar();
        string s;
        getline(cin, s);
        cout << qi << ": " << s << endl;
        map<string, set<int>> *mp;
        switch (qi)
        {
        case 1:
            mp = &titles;
            break;
        case 2:
            mp = &authors;
            break;
        case 3:
            mp = &keywords;
            break;
        case 4:
            mp = &publishers;
            break;
        case 5:
            mp = &years;
            break;
        }
        query(*mp, s);
    }
    return 0;
}
Subscribe
提醒
guest
0 评论
Inline Feedbacks
View all comments