题解 | 查找兄弟单词
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<string> sour;
vector<string> dst;
bool isBrother(string k, string s){
if(k.length() != s.length() || k == s) return false;
sort(s.begin(),s.end());
sort(k.begin(),k.end());
return k == s;
}
int main(void){
int n;
cin >> n;
while(n--){
string tmp;
cin >> tmp;
sour.push_back(tmp);
}
string pat;
cin >> pat;
int k;
cin >> k;
for(auto elem : sour){
if(isBrother(pat,elem)){
dst.push_back(elem);
}
}
sort(dst.begin(),dst.end());
cout << dst.size() << endl;
if(dst.size() >= k) cout << dst[k - 1] << endl;
return 0;
}
查看17道真题和解析