Submission

Status:

[PP-SS][PPPPP]

Subtask/Task Score:

{0/50}{50/50}

Score: 50

User: sulinx

Problemset: จุดแวะพัก

Language: cpp

Time: 0.007 second

Submitted On: 2026-02-15 13:39:43

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

int main(){
    ios_base::sync_with_stdio(false),cin.tie(nullptr);
    
    int n,k;
    cin >> n >> k;

    priority_queue<pair<int,string>> pq;
    
    for(int i = 0;i<n;i++){
        string s;
        getline(cin,s);

        string name = "";
        int count = 0;
        int tmp = 0;
        bool found = false;

        for(int j = 0;j<s.length();j++){
            char c = s[j];
            if(isalpha(c)){
                name += c;
            }
            else if(isdigit(c)){
                if(!isdigit(s[j-1])){
                    count++;
                    tmp = 0;
                }
                tmp = tmp*10+(c-'0');
            }
            else{
                if(!found && j > 0 && isdigit(s[j-1]) && tmp == k){
                    if(pq.size() < 3 || pq.top().first > count ||
                        (pq.top().first == count && pq.top().second > name)){
                        pq.push({count,name});
                        if(pq.size() > 3) pq.pop();
                    }
                    found = true;
                }
            }
        }
        int len = s.length();
        if(!found && len > 0 && isdigit(s[len-1]) && tmp == k){
                    if(pq.size() < 3 || pq.top().first > count ||
                        (pq.top().first == count && pq.top().second > name)){
                        pq.push({count,name});
                        if(pq.size() > 3) pq.pop();
                    }
        }
    }

    if(pq.empty()){
        cout << -1;
    }
    else{
        vector<string> res;
        while(!pq.empty()){
            res.push_back(pq.top().second);
            pq.pop();
        }
    
        for(int i = res.size() - 1; i >= 0; i--){
        cout << res[i] << ' ';
        }
    }
}