题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream> #include <vector> #include <string> #include <set> using namespace std; bool isBrother(const string &s1, const string&s2) { if(s1.length() != s2.length() || s1 == s2) return false; multiset<char> s1_set; for(char c : s1) { s1_set.insert(c); } for(char c : s2) { if(s1_set.find(c) != s1_set.end()) s1_set.erase(s1_set.find(c)); else return false; } return true; } int main() { int n, k; string tar; cin >> n; vector<string> dic(n); // 字典 for(int i=0; i<n; ++i) cin >> dic[i]; cin >> tar >> k; multiset<string> broWords; // 利用set排序 for(int i=0; i<n; ++i) { if(isBrother(tar, dic[i])) broWords.insert(dic[i]); } int cnt = 1; cout << broWords.size() << endl; for(auto word : broWords) { if(k == cnt) { cout << word; break; } ++cnt; } return 0; } // 64 位输出请用 printf("%lld")