题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
bool isBrother(string a, string b) {
if (a != b) {
sort(a.begin(), a.end());
sort(b.begin(), b.end());
return a == b;
} else {
return false;
}
}
int main() {
int n, k;
string x;
vector<string> strs;
multiset<string> Set;
cin >> n;
while (n--) {
string str;
cin >> str;
strs.push_back(str);
}
cin >> x;
cin >> k;
for (string s : strs)
if (isBrother(s, x))
Set.insert(s);
cout << Set.size() << endl;
if (k >= 1 && k <= Set.size()) {
set<string>::iterator iter = Set.begin();
while (--k)
iter++;
cout << *iter << endl;
}
} 

查看14道真题和解析