1022.Digital Library (30)...to be continued...

1022.Digital Library (30)…to be continued…

pat-al-1022

2017-02-20

  • 关于map的用法:如果是一一对应关系,直接令mp[index] = content就可以;如果是一对多,就要用到mp[index].insert
  • getline的用法:getline(cin, input)。别忘了如果前一个输入是scanf,要读掉换行
  • map和set的方法中都有find
  • 判断一个查询内容是否存在可以用:if(mp.find(queryContent) == mp.end()),或者if(mp.count(queryContent) == 0),或者if(mp[queryContent].size() == 0)
/**
 * pat-al-1022
 * 2017-02-20
 * Cpp version
 * Author: fengLian_s
 */
#include<stdio.h>
#include<iostream>
#include<map>
#include<set>
#include<string>
using namespace std;
map<string, set<int> > mpTitle, mpAuthor, mpKey, mpPub, mpYear;
void query(map<string, set<int> >& mp, string& queryContent)
{
  if(mp[queryContent].size() == 0)
  {
    printf("Not Found\n");
  }
  else
  {
    for(set<int>::iterator it = mp[queryContent].begin();it != mp[queryContent].end();it++)
    {
      printf("%07d\n", *it);
    }
  }
}
int main()
{
  freopen("in.txt", "r", stdin);
  int n, m, id, type;
  string title, author, key, pub, year;
  scanf("%d", &n);
  for(int i = 0;i < n;i++)
  {
    scanf("%d\n", &id);
    getline(cin, title);
    //cout << "title = " << title << endl;
    mpTitle[title].insert(id);
    getline(cin, author);
    //cout << "author = " << author << endl;
    mpAuthor[author].insert(id);
    while(cin >> key)
    {
      char c = getchar();
      mpKey[key].insert(id);
      //cout << "key = " << key << endl;
      if(c == '\n')
        break;
    }
    getline(cin, pub);
    //cout << "pub = " << pub << endl;
    mpPub[pub].insert(id);
    getline(cin, year);
    //cout << "year = " << year << endl;
    mpYear[year].insert(id);
  }
  scanf("%d", &m);
  string queryContent;
  for(int i = 0;i < m;i++)
  {
    scanf("%d: ", &type);
    getline(cin, queryContent);
    printf("%d: ", type);
    cout << queryContent << endl;
    if(type == 1)
      query(mpTitle, queryContent);
    else if(type == 2)
      query(mpAuthor, queryContent);
    else if(type == 3)
      query(mpKey, queryContent);
    else if(type == 4)
      query(mpPub, queryContent);
    else
      query(mpYear, queryContent);
  }
  return 0;
}

-TBC-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值