您的位置:首页 > 其它

1022. Digital Library (30) @ PAT (Advanced Level) Practise

2013-08-28 13:37 495 查看
考察倒排索引

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<sstream>
using namespace std;

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
//build map
map<string,std::vector<string>> queryMap[5];
while(n--)
{
getchar();
string id, title, author, wordList, publisher, year;
getline(cin, id);
getline(cin, title); getline(cin, author);
getline(cin, wordList);
getline(cin, publisher); cin>>year;
queryMap[0][title].push_back(id); queryMap[1][author].push_back(id);
queryMap[3][publisher].push_back(id); queryMap[4][year].push_back(id);
istringstream istr(wordList);
while(!istr.eof())
{
string word;
istr>>word;
queryMap[2][word].push_back(id);
}
}
//sort first
for(int i = 0; i < 5; ++i)
{
map<string,std::vector<string>>::iterator it;
for(it=queryMap[i].begin(); it!=queryMap[i].end(); it++)
sort(it->second.begin(), it->second.end());
}
//query
int m;
scanf("%d",&m);
while(m--)
{
int index; string q;
scanf("%d: ", &index);
getline(cin, q);
printf("%d: ", index);
cout<<q<<endl;
index--;
map<string,std::vector<string>>::iterator it;
it = queryMap[index].find(q);
if(it!=queryMap[index].end())
{
std::vector<string> result = queryMap[index][q];
for(int i = 0; i < result.size(); ++i)
cout<<result[i]<<endl;
}
else printf("Not Found\n");

}
}
return 0;
}


from:
http://blog.csdn.net/sunbaigui/article/details/8657103
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: