题解 | 记票统计

记票统计

https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894

/*#牛客春招刷题训练营# https://www.nowcoder.com/discuss/726480854079250432*/
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<pair<string, int>> a(n,{"", 0});
  for (int i = 0; i < n; i++){
    cin >> a[i].first;
  }
  string s;
  int num = 0;
  int x;
  cin >> x;
  while(x--){
    cin >> s;
    if (find_if(a.begin(), a.end(),[&](const auto& p){return p.first == s;}) != a.end()){
      int index = distance(a.begin(), find_if(a.begin(), a.end(),[&](const auto& p){return p.first == s;}));//---------find_if()可自定义查找的函数,当return 1 时会返回当前的迭代器;distance计算两迭代器之间的距离
      a[index].second++;
    }
    else num++;
  }
  for (int i = 0; i < n; i++){
    cout << a[i].first << " : " << a[i].second << endl;
  }
  cout << "Invalid : " << num;
}
// 64 位输出请用 printf("%lld")

#写题解领奖励##牛客春招刷题训练营#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务