éè¨å¦çããä¸ã§ãå度大ããé ã«ã½ã¼ãããåé¡
åé¡æ¦è¦
åã®æåå ãä¸ããããã
ãã®åã«ä¸å以ä¸åºç¾ããåèªãããã®åºç¾åæ°ã®å¤ãé ã«ä¸¦ã¹ãã¨ã çªç®ã®åèªãåºåããï¼ä¸æã«æ±ºã¾ããªãå ´å㯠"ambiguous" ã¨åºåããï¼ã
å¶ç´
èãããã¨
ã¾ãã¯éè¨å¦çãããï¼ãããªãã¡ã次ã®ãããªé£æ³é
å (C++ ãªãã° map<string, int>
åãæ±ãããã
nums[str]
ï¼ åã®æååã®ä¸ã«ãæååstr
ãä½åããã
ãããæ±ãããã¨ã¯ãåæååãã¨ã«åã³ (åæ°, æåå) ã®ãã¢ãé åã«æ ¼ç´ãã¦ããããããã大ããé ã«ã½ã¼ãã㦠çªç®ãæ±ããã°è¯ãã
ãã ããã¿ã¤ãããå ´å㯠"AMBIGUOUS" ã¨è¿ãå¿ è¦ããããããã§ã¯ã次ã®ããã«å¤å®ããã
- çªç®ã«å¤§ããé »åº¦ã¨ã çªç®ã«å¤§ããé »åº¦ãä¸è´ãã ( ã®å ´å)
- çªç®ã«å¤§ããé »åº¦ã¨ã çªç®ã«å¤§ããé »åº¦ãä¸è´ãã ( ã®å ´å)
ã«ã¤ãã¦ã¯ã "AMBIGUOUS" ã¨è¿ãã°ããã
å ¨ä½ã¨ãã¦ãæååã®é·ãã®æ大å¤ã ã¨ãã¦ãè¨ç®é㯠ã¨è©ä¾¡ã§ããã
ã³ã¼ã
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K, K--; string S; map<string, int> nums; for (int i = 0; i < N; i++) { cin >> S; nums[S]++; } vector<pair<int, string>> v; for (auto [str, num] : nums) v.emplace_back(num, str); sort(v.begin(), v.end()); reverse(v.begin(), v.end()); if (K > 0 && v[K].first == v[K-1].first) cout << "AMBIGUOUS" << endl; else if (K+1 < v.size() && v[K].first == v[K+1].first) cout << "AMBIGUOUS" << endl; else cout << v[K].second << endl; }