#36699: C++


samlin961112@gmail.com (林哲甫)

學校 : 新北市私立南山高級中學
編號 : 220506
來源 : [219.70.213.92]
最後登入時間 :
2024-05-06 16:41:02
j227. 我愛蝸牛大贈獎 -- 板橋高中Python教學題 | From: [219.70.213.92] | 發表日期 : 2023-08-03 17:07

#include <bits/stdc++.h>
using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  string required_words, collected_words;
  getline(cin, required_words);
  getline(cin, collected_words);

  // 使用哈希表來計算每個單字出現的次數
  unordered_map<string, int> required_count, collected_count;

  stringstream ss_required(required_words);
  string word;
  while (ss_required >> word) {
    required_count[word]++;
  }

  stringstream ss_collected(collected_words);
  while (ss_collected >> word) {
    collected_count[word]++;
  }

  // 檢查是否可以兌換公仔
  int max_exchange = INT_MAX;
  for (auto& kv : required_count) {
    string word = kv.first;
    int required = kv.second;
    int collected = collected_count[word];

    if (collected == 0) {
      max_exchange = 0;
      break;
    }

    max_exchange = min(max_exchange, collected / required);
  }

  cout << max_exchange << '\n';
}

 
ZeroJudge Forum