1022. Digital Library (30)(字符串分割) 模拟
2016-03-06 12:19
549 查看
1022. Digital Library (30)
时间限制1000 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueA 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 supposedto 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 title2: name of an author3: a key word4: name of a publisher5: a 4-digit number representing the yearOutput 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: blablablaSample 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
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;}
相关文章推荐
- POSIX信号量与互斥锁
- LVS-NAT 模式配置
- Python---装饰器
- AOP
- 软件测试 作业一 (印象深刻的一些错误)
- 以用户名注册为例分析三种Action获取数据的方式
- Git同时提交到多个远程仓库
- JS、javascript获取当前时间戳的方法
- 文件
- RHEL 7.0 yum源
- POJ 1556 The Doors(计算几何+最短路)
- java设计模式学习笔记第三章
- 关于软件工程
- java dom4j 曾删改查、编码XML文件
- C++ UML Tips
- 白话Spring(基础篇)---编程式事务(1)
- poj1102数字打印,七段数字码
- irtualbox工具栏消失问题
- java设计模式学习笔记第二章
- 阅读计划