PAT 1022

题目 : Digital Library

分值 : 30
难度 : 中等STL题
思路 : 单纯的STL题,结构体搞出来暴搜就行,我一开始想多了,后来C++的split了解了一下,
       还有就是 利用map 映射 <string,vector<string> >  了解一下,这样不用管主键
       是否重叠,只要考虑副键是否有重叠可能就行。
坑点 : 一开始觉得 暴搜肯定超时,结果就是暴搜做。
评语 : 以后不要想太多,想到做法先动手弄出来再说。

istringstream 了解一下

1
2
3
4
5
#includ <sstream>  // istringstream 必须要用的
getline(cin , keys) ;
istringstream is(keys) ;
string temp ;
while(is >> temp) Map[temp].push_back(id) ;

具体代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <map>
#include <vector>
using namespace std ;
map <string ,vector<string>> Map ;
typedef struct Node{
string id ;
string title ;
string author ;
string publisher;
string year ;
}Nodes ;
Nodes db[10000] ;
bool cmp(Node a , Node b )
{
return a.id < b.id ;
}
int main()
{
int N ;
cin >> N ;
string keys ,temp ;
for(int i = 0 ; i< N ; i++)
{
cin >> db[i].id ;
getchar() ;
getline(cin ,db[i].title );
getline(cin ,db[i].author );
getline(cin ,keys );
istringstream is(keys);
while(is>>temp)
Map[temp].push_back(db[i].id) ;
getline(cin ,db[i].publisher );
getline(cin ,db[i].year );
}
sort(db,db+N ,cmp);
//for(int i = 0 ; i<N ;i++)
// cout<<db[i].id<<endl ;
int M ;
cin>>M ;
int flag ;
int chose ;
for(int i = 0 ; i< M ; i++)
{
string query ;
flag=0;
scanf("%d: ",&chose);
getline(cin,query);
cout<<chose<<": "<<query<<endl;
int flag = 0 ;
switch(chose){
case 1:
{
for(int j = 0 ; j < N ;j++)
if(db[j].title == query)
{
cout<< db[j].id <<endl ;
flag = 1 ;
}
break ;
}
case 2:
{
for(int j = 0 ; j < N ;j++)
if(db[j].author == query)
{
cout<< db[j].id <<endl ;
flag = 1 ;
}
break ;
}
case 3:
{
if(Map[query].size() ==0)
{
flag = 0 ; break ;
}
flag = 1 ;
sort(Map[query].begin() , Map[query].end()) ;
vector <string>::iterator itea = Map[query].begin() ;
for(;itea!=Map[query].end() ; itea++)
cout <<*itea <<endl ;
break ;
}
case 4:
{
for(int j = 0 ; j < N ;j++)
if(db[j].publisher == query)
{
cout<< db[j].id <<endl ;
flag = 1 ;
}
break ;
}
case 5:
{
for(int j = 0 ; j < N ;j++)
if(db[j].year == query)
{
cout<< db[j].id <<endl ;
flag = 1 ;
}
break ;
}
}
if(!flag)
cout << "Not Found"<<endl ;
}
}