题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream> #include <set> #include <string> #include <vector> #include <algorithm> using namespace std; bool is_brother(string word1, string word2) { if (word1.size() != word2.size()) return false; if(word1 == word2) return false; sort(word1.begin(), word1.end()); sort(word2.begin(), word2.end()); if(word1 == word2) return true; else return false; } void num_bro(vector<string> vec, string str , int k) { int ans = 0; multiset<string> bro; for (auto it : vec) { if (is_brother(it, str)) { ans++; bro.insert(it); } } cout << ans << endl; if (ans >= k) { auto it = bro.begin(); advance(it, k - 1); cout << *it << endl; } } int main() { int n, k; vector<string> vec; string str; cin >> n; while (n--) { cin >> str; vec.push_back(str); } cin >> str; cin >> k; num_bro(vec, str, k); return 0; }