【PAT Advanced Level】1022. Digital Library (30)
2013-11-07 18:51
525 查看
这题其他没什么,就是输入输出比较麻烦,还怪自己太不熟练。
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <fstream> #include <string.h> using namespace std; struct book { string id; string title; string author; vector<string> keywords; string publisher; string puYear; book(string i, string t, string a, vector<string> k, string pb, string py):id(i), title(t), author(a), keywords(k), publisher(pb), puYear(py) {} }; vector<string> split(string &s, const char *c) { char *cstr, *p; vector<string> res; cstr = new char[s.size() + 1]; strcpy(cstr, s.c_str()); p = strtok(cstr, c); while(p != NULL) { res.push_back(p); p = strtok(NULL, c); } return res; } vector<book> v; bool find(vector<string> &res, int p, string inp) { for(int i = 0; i < v.size(); i++) { if(p == 1) { if(v[i].title == inp) res.push_back(v[i].id); } else if(p == 2) { if(v[i].author == inp) res.push_back(v[i].id); } else if(p == 3) { if(find(v[i].keywords.begin(), v[i].keywords.end(), inp) != v[i].keywords.end()) res.push_back(v[i].id); } else if(p == 4) { if(v[i].publisher == inp) res.push_back(v[i].id); } else if(p == 5) { if(v[i].puYear == inp) res.push_back(v[i].id); } } if(res.empty()) return false; return true; } void output(string &s) { cout<<s<<endl; } int main() { // fstream cin("a.txt"); // freopen("a.txt", "r", stdin); int n; cin>>n; string id; string title; string author; string keywords; string publisher; string puYear; char *c = new char(' '); string useless; getline(cin, useless); while (n--) { getline(cin, id); getline(cin, title); getline(cin, author); getline(cin, keywords); getline(cin, publisher); getline(cin, puYear); book b(id, title, author, split(keywords, c), publisher, puYear); v.push_back(b); } int query; cin>>query; while (query--) { int index; string s; char tmp; cin>>index>>tmp; getline(cin, s); s.erase(s.begin(), s.begin() + 1); int id = 0; vector<string> res; if(find(res, index, s)) { sort(res.begin(), res.end()); } cout<<index<<": "<<s<<endl; if(res.empty()) cout<<"Not Found"<<endl; else { for_each(res.begin(), res.end(), output); } } }
相关文章推荐
- poj 2182,乱搞
- Word中无法输入中文
- 如何获取view的宽度和高度
- 简单九步,完成Ubuntu(BackTrack5)下Fcitx中文输入法的安装
- 极路由饥饿营销引质疑 联合创始人拿数据正面回应
- hadoop上的C++程序开发
- hadoop上的C++程序开发
- 【cocos2d-x入门实战】环境配置、交叉编译及安卓项目的生成
- 安装Ubuntu nfs配置系统
- the difference between fopen&open
- LSH local sensitive hash 局部敏感哈希简单理解
- PHPExcel常用方法汇总
- The Google File System
- SQL Server 批量插入数据的两种方法
- JavaScript 遍历JSON对象
- linux UNIX socket编程
- setLayout(null)的意思和JLabel设置背景色
- HDU 1086 You can Solve a Geometry Problem too(求线段交点数)
- Fork + Pull模式
- 释放MySQL ibdata1文件的空间