PAT习题解:1022. Digital Library (30)
2016-02-18 20:52
369 查看
题目链接:http://www.patest.cn/contests/pat-a-practise/1022
这道题目我没想到什么好办法做,最后参考了闲云阁的文章,用map 和 set 做出来,算是顺便学习了一下 STL
里面 map 和 set 的用法。话说C++的输入输出真是麻烦啊,这题用Python做的话写起来会很简单吧。
#define _CRT_SECURE_NO_WARNINGS
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
map<string, set<string>> bookname;
map<string, set<string>> author;
map<string, set<string>> keywords;
map<string, set<string>> publisher;
map<string, set<string>> year;
void printSearchResults(map<string, set<string>> &m, string query)
{
if (m[query].size() > 0)
{
for (set<string>::iterator it = m[query].begin(); it != m[query].end(); it++)
{
cout << *it << endl;
}
}
else
{
cout << "Not Found" << endl;
}
}
int main()
{
freopen("d:\\in.txt", "r", stdin);
char c;
string sid;
string s;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> sid;
c = getchar();
getline(cin, s);
bookname[s].insert(sid);
getline(cin, s);
author[s].insert(sid);
char str[105];
cin.getline(str, 105);
char *p = strtok(str," ");
while (p)
{
s = p;
keywords[s].insert(sid);
p = strtok(NULL, " ");
}
getline(cin, s);
publisher[s].insert(sid);
getline(cin, s);
year[s].insert(sid);
}
cin >> n;
int item;
while (n--)
{
scanf("%d: ", &item);
getline(cin, s);
cout << item << ": " << s << endl;
switch (item)
{
case 1:
printSearchResults(bookname, s);
break;
case 2:
printSearchResults(author, s);
break;
case 3:
printSearchResults(keywords, s);
break;
case 4:
printSearchResults(publisher, s);
break;
case 5:
printSearchResults(year, s);
break;
}
}
return 0;
}
这道题目我没想到什么好办法做,最后参考了闲云阁的文章,用map 和 set 做出来,算是顺便学习了一下 STL
里面 map 和 set 的用法。话说C++的输入输出真是麻烦啊,这题用Python做的话写起来会很简单吧。
#define _CRT_SECURE_NO_WARNINGS
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
map<string, set<string>> bookname;
map<string, set<string>> author;
map<string, set<string>> keywords;
map<string, set<string>> publisher;
map<string, set<string>> year;
void printSearchResults(map<string, set<string>> &m, string query)
{
if (m[query].size() > 0)
{
for (set<string>::iterator it = m[query].begin(); it != m[query].end(); it++)
{
cout << *it << endl;
}
}
else
{
cout << "Not Found" << endl;
}
}
int main()
{
freopen("d:\\in.txt", "r", stdin);
char c;
string sid;
string s;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> sid;
c = getchar();
getline(cin, s);
bookname[s].insert(sid);
getline(cin, s);
author[s].insert(sid);
char str[105];
cin.getline(str, 105);
char *p = strtok(str," ");
while (p)
{
s = p;
keywords[s].insert(sid);
p = strtok(NULL, " ");
}
getline(cin, s);
publisher[s].insert(sid);
getline(cin, s);
year[s].insert(sid);
}
cin >> n;
int item;
while (n--)
{
scanf("%d: ", &item);
getline(cin, s);
cout << item << ": " << s << endl;
switch (item)
{
case 1:
printSearchResults(bookname, s);
break;
case 2:
printSearchResults(author, s);
break;
case 3:
printSearchResults(keywords, s);
break;
case 4:
printSearchResults(publisher, s);
break;
case 5:
printSearchResults(year, s);
break;
}
}
return 0;
}
相关文章推荐
- EventBus3.0使用总结
- EventBus3.0使用总结
- EventBus3.0使用总结
- 斐波那契数列 -- 递归算法(-)
- 不错的判断 UITextView 内容不超过20个字符串的方法
- EventBus3.0使用总结
- 安卓作业--第三篇
- 和为S的连续正数序列
- 第十二章编程练习(3)
- 单例的两种实现方式
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- nginx 配置文件配置
- 子类初始化那点事
- pp看书笔记---C#高级编程第九版 第八章 【委托、Lambda表达式和事件】
- adb被占用解决办法
- 1004. Counting Leaves (30)
- C++11的mutex和lock_guard,muduo的MutexLock 与MutexLockGuard
- C/C++ 判断文件夹是否存在以及创建、删除文件夹 windows以及linux通用
- leetcode:Odd Even Linked List
- 再见2015、hello,2016