1022 Digital Library (30分)

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:

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

思路:

主要考察string的输入和处理(格式非常丰富),以及map的使用
1.输入:
这道题目的string输入主要用到getline:

//getchar();     如果缓冲区内有残留的'\n',则需要先吸收
string str;
getline(cin,str);
//读取一整行,str1为当前缓冲区里,'\n'前的所有内容,
//且读取之后'\n'不会残留缓冲区 

也可以用c风格的正则格式化输入:(会很麻烦)

string str;
str.resize(20);//必须先分配空间
scanf("%[^\n]", &str[0]);//读入'\n'前的所有字符串,若长度不满20,其余内容全为'\0'
str.erase(find(str.begin(),str.end(),'\0'),str.end());//清除所有多余的'\0'
//getchar();     和getline()不同,scnaf的读入会残留'\n'

一行中多个以空格分隔的key words的读入可以用下面这种:

string words;
 while(cin >> words){ 
 	//处理....
    if(getchar()=='\n') break;
}

2.map:
建立5个map,分别对应输入的5类信息,map的键为string,值为一个set,存放所有具有该特征string的书的id(因为set可以避免重复id,而且会自动升序)
为了统一五个map,年份和其他信息一样用string存储会比较好,便于后续queries.
最后还有一个坑点:输出的id必须占7位,不足则前导补0(3、4测试点)

源码:

#include<bits/stdc++.h>

#define nmax 81
using namespace std;
map<string, set<int> > str_im[5];
int n, m;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        string tt, au, pb, year;
        //书名、作者、出版社、年份
        int id;
        scanf("%d", &id);
        getchar();//有回车符残留
        getline(cin,tt);
        getline(cin,au);
        //关键字提取、映射
        string t;
        while(cin>>t)
        {
            str_im[2][t].insert(id);
            if(getchar()=='\n')break;
        }
        getline(cin,pb);
        getline(cin,year);
        str_im[0][tt].insert(id);//title
        str_im[1][au].insert(id);//author
        str_im[3][pb].insert(id);//publisher
        str_im[4][year].insert(id);//year
    }

    cin >> m;
    getchar();
    for (int i = 0; i < m; i++) {
        string t;
        getline(cin,t);
        printf("%s\n", t.c_str());
        int Index = t[0] - '1';//map索引
        string temp = string(t.begin() + 3, t.end());//提取查询信息
        if (!str_im[Index].count(temp)) {
            printf("Not Found\n");
        } else {
            auto beg = str_im[Index][temp].begin();//头
            auto ed = str_im[Index][temp].end();//尾
            for (auto it = beg; it != ed; it++) {
                printf("%07d\n", *it);//按照格式输出
            }
        }
    }
}

复杂处理版本(训练一下stl):

#include<bits/stdc++.h>

#define nmax 81
#define Max
using namespace std;
map<string, set<int> > str_im[5];
int n, m;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        string tt, au, pb, year;
        int id;
        //用scanf必须先分配内存,十分麻烦
        tt.resize(nmax);
        au.resize(nmax);
        pb.resize(nmax);
        year.resize(nmax);
        scanf("%d", &id);
        getchar();
        scanf("%[^\n]", &tt[0]);
        getchar();
        scanf("%[^\n]", &au[0]);
        getchar();
        //关键字提取、映射,如果不用上述while的读入的话这里处理会及其麻烦
        string t;t.resize(60);
        scanf("%[^\n]",&t[0]);
        auto x = t.begin();
        auto y = find(t.begin(),t.end(),'\0');
        auto last = x;
        while (1) {
            x = find(x, y, ' ');//找到下一个空格的位置
            string word = string(last, x);//提取关键字
            str_im[2][word].insert(id);//map映射
            if (x == y)//所有关键字读取完毕
                break;
            else
                last = ++x;//下一个关键字
        }
        getchar();
        scanf("%[^\n]", &pb[0]);
        getchar();
        scanf("%[^\n]", &year[0]);
        //如果不用getline读入,这里的处理也会很麻烦
        tt.erase(find(tt.begin(), tt.end(), '\0'), tt.end());
        au.erase(find(au.begin(), au.end(), '\0'), au.end());
        pb.erase(find(pb.begin(), pb.end(), '\0'), pb.end());
        year.erase(find(year.begin(), year.end(), '\0'), year.end());
        str_im[0][tt].insert(id);
        str_im[1][au].insert(id);
        str_im[3][pb].insert(id);
        str_im[4][year].insert(id);
    }

    cin >> m;
    getchar();
    for (int i = 0; i < m; i++) {
        string t;
        t.resize(90);
        scanf("%[^\n]", &t[0]);
        getchar();
        int Index = t[0] - '1';
        string temp = string(t.begin() + 3, find(t.begin(),t.end(),'\0'));
        printf("%s\n", t.c_str());
        if (!str_im[Index].count(temp)) {
            printf("Not Found\n");
        } else {
            auto beg = str_im[Index][temp].begin();
            auto ed = str_im[Index][temp].end();
            for (auto it = beg; it != ed; it++) {
                printf("%07d\n", *it);
            }
        }
    }
}

总结:

拿到题目不要忙着去做吧,先分析一下策略,仔细、耐心多读读题目。像这道题目,输入方法选的对的话代码量和时间都会明显省好多。
总之主要训练了一下string的处理,刚开始犯傻用scanf读入,主要是考虑到大数据的时候,cin可能会超时。后来才发现。。。
在这里插入图片描述
f**k!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值