您的位置:首页 > 其它

solution Of 1022. Digital Library (30)

2016-06-20 22:49 267 查看
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 (<=10000) 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

结题思路 :

题意要求我们关于对应输入的结果列表。

要求1:需要对关键字部分的输入进行分割;

要求2:考虑到最后输出的排序,对于id用string保存,接下去要读入一整行,单之前还有一个回车存储在输入缓冲区,所以此时需要用getchar()来吃掉回车。

程序步骤:

第一步、根据输入建立用户列表;

第二步、根据输入将满足要求的图书编号放入到结果列表中;

第三部、排序输出即可。

具体程序(AC)如下:

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
map<string,int> no2Str;
auto ip=no2Str.begin();//auto变量的初始化
struct node{
string id;
char title[90];
char author[90];
vector<int> key;
char publisher[90];
char year[10];
};
int seed=0;
void splitAndStore(node& cur,string words)
{
string separate_char=" ";
int slen=1;
int start=0,index=-1;
string part;
while((index=words.find(separate_char,start))!=-1)
{
part=words.substr(start,index-start);
ip=no2Str.find(part);
if(ip==no2Str.end())
no2Str[part]=seed++;
cur.key.push_back(no2Str[part]);
start=index+slen;
}
part=words.substr(start);
if(!part.empty())
{
ip=no2Str.find(part);
if(ip==no2Str.end())
no2Str[part]=seed++;
cur.key.push_back(no2Str[part]);
}

}
vector<node> listP;
void getNo(int choice,int n)
{
vector<string> result;
char query[90];
auto ip=no2Str.begin();
cin.getline(query,90);
cout<<choice<<": "<<query<<endl;
if(choice==1)
{
for(int i=0;i<n;++i)
if(!strcmp(listP[i].title,query))
result.push_back(listP[i].id);
}
else if(choice==2)
{
for(int i=0;i<n;++i)
if(!strcmp(listP[i].author,query))
result.push_back(listP[i].id);
}
else if(choice==3)
{
ip=no2Str.find(string(query));
if(ip!=no2Str.end())
{
int ind=no2Str[string(query)];
for(int i=0;i<n;++i)
{
for(int j=0;j<listP[i].key.size();++j)
{
if(listP[i].key[j]==ind)
{
result.push_back(listP[i].id);
break;
}
}
}
}
}
else if(choice==4)
{
for(int i=0;i<n;++i)
if(!strcmp(listP[i].publisher,query))
result.push_back(listP[i].id);
}
else
{
for(int i=0;i<n;++i)
if(!strcmp(listP[i].year,query))
result.push_back(listP[i].id);
}
if(result.size()==0)
cout<<"Not Found"<<endl;
else
{
sort(result.begin(),result.end());
for(int i=0;i<result.size();++i)
cout<<result[i]<<endl;
}
}
int main()
{
int N,M;
cin>>N;
listP.clear();
listP.resize(N);
string words;
for(int i=0;i<N;++i)
{
cin>>listP[i].id;
getchar();
cin.getline(listP[i].title,90);
cin.getline(listP[i].author,90);
getline(cin,words);
splitAndStore(listP[i],words);
cin.getline(listP[i].publisher,90);
cin.getline(listP[i].year,10);
}
cin>>M;
int choice;
for(int i=0;i<M;++i)
{
cin>>choice;
getchar();
getchar();
getNo(choice,N);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: