#include <iostream> #include <queue> #include <vector> using namespace std; bool isBroWord(vector<int>& count,string& word, string& pat){ if(word.size() != pat.size() || word == pat) return false; vector<int> cnt(26); for(char c: pat){ cnt[c - 'a']++; } for(int i = 0; i < 26; i++){ if(cnt[i] != count[i]){ return false; } } return true; } int main() { int n, k; cin >> n; vector<string> dict(n); for(int i = 0; i < n; i++){ cin >> dict[i]; } string word; cin >> word >> k; priority_queue<string, vector<string>, less<string>> pq; int cnt = 0; vector<int> count(26); for(char c: word){ count[c - 'a']++; } for(int i = 0; i < n; i++){ if(isBroWord(count,word,dict[i])){ cnt++; pq.push(dict[i]); if(pq.size() > k){ pq.pop(); } } } cout << cnt << endl; if(pq.size() == k){ cout << pq.top() << endl; } } // 64 位输出请用 printf("%lld")
看了一圈题解,就没有人用优先队列吗