题解 | 查找兄弟单词
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
bool isBrother(const string & str, const string& x){
if (str.size() != x.size() || str == x)
return false;
vector<int> str_vec(26,0);
vector<int> x_vec(26,0);
for (const auto i : str) {
++str_vec[i-'a'];
}
for (const auto i : x) {
++x_vec[i-'a'];
}
return str_vec == x_vec;
}
int main() {
int n, k; cin >> n;
vector<string> strs, bros;
string t, x;
while(n-- > 0) {
cin >> t;
strs.push_back(t);
}
cin >> x >> k;
for(const auto& it : strs){
if (isBrother(it, x)) {
bros.push_back(it);
}
}
sort(bros.begin(), bros.end());
cout << bros.size();
if (bros.size() >= k) {
cout << "\n" << bros.at(k-1);
}
}
查看3道真题和解析