1022. Digital Library (30)(字符串分割) 模拟
来源:互联网 发布:网络红人丁小雨 编辑:程序博客网 时间:2024/04/29 07:53
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:31111111The Testing BookYue Chentest code debug sort keywordsZUCS Print20113333333Another Testing BookYue Chentest code sort keywordsZUCS Print220122222222The Testing BookCYLLkeywords debug bookZUCS Print2201161: The Testing Book2: Yue Chen3: keywords4: ZUCS Print5: 20113: blablablaSample Output:
1: The Testing Book111111122222222: Yue Chen111111133333333: keywords1111111222222233333334: ZUCS Print11111115: 2011111111122222223: blablablaNot Found
AC代码
#include<iostream>#include<vector>#include<algorithm>#include<map>using namespace std;struct Node{ string ID;};vector<string> StrSplit(string str,char ch){ vector<string> result; int pos = 0; for (int i=0; i<str.size(); ++i) { pos = (int)str.find(ch,i); if(pos<str.size()){//找到字符串. string te = str.substr(i,pos-i); result.push_back(te); i = pos; }else{ string te = str.substr(i,str.size()-1); result.push_back(te); break; } } return result;}vector<Node>allBooks;bool cmp(int a,int b){ return allBooks[a].ID<allBooks[b].ID;}int main(){ int N,M,kind; scanf("%d%*c",&N); map<string, vector<int>> authorMap,keyWordMap,titleMap,publisherMap,yearMap; string title,author,keystr,publisher,year,query; vector<string>curKeys; allBooks.resize(N); for(int i=0;i<N;++i) { getline(cin,allBooks[i].ID); getline(cin, title); titleMap[title].push_back(i); getline(cin, author); authorMap[author].push_back(i); getline(cin, keystr); vector<string> keys = StrSplit(keystr,' '); for (int j=0; j<keys.size();++j) keyWordMap[keys[j]].push_back(i); getline(cin, publisher); publisherMap[publisher].push_back(i); getline(cin, year); yearMap[year].push_back(i); } scanf("%d",&M); for (int i=0; i<M;++i) { scanf("%d: ",&kind); getline(cin, query); printf("%d: %s\n",kind,query.c_str()); vector<int>res; switch (kind) { case 1: res.assign(titleMap[query].begin(),titleMap[query].end()); break; case 2: res.assign(authorMap[query].begin(),authorMap[query].end()); break; case 3: res.assign(keyWordMap[query].begin(),keyWordMap[query].end()); break; case 4: res.assign(publisherMap[query].begin(),publisherMap[query].end()); break; case 5: res.assign(yearMap[query].begin(),yearMap[query].end()); break; } if (res.size()==0) printf("Not Found\n"); else{ sort(res.begin(),res.end(),cmp); for (int j=0; j<res.size(); ++j) printf("%s\n",allBooks[res[j]].ID.c_str()); } } return 0;}
- 1022. Digital Library (30)(字符串分割) 模拟
- 1022. Digital Library (30) -map -字符串处理
- 1022. Digital Library (30) 字符串处理题
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- 1022. Digital Library (30)
- Git同时提交到多个远程仓库
- RHEL 7.0 yum源
- Codeforces 106C Buns 【0-1背包】
- 文件
- String详解
- 1022. Digital Library (30)(字符串分割) 模拟
- hdu 5637 BestCoder Round #74 (div.2)
- [转载] Java Iterator迭代器详解
- 安卓学习笔记之跑马灯实现
- HDU 1846 Brave Game
- C++设计模式全解析
- Android apk动态加载机制的研究【转】
- Win7 64位,安装 PL/SQL Developer
- redis3.0.0 集群安装详细步骤-【超人学院】