题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <algorithm>
#include <cstring>
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
int main() {
int n;
cin >>n;
vector<string> input(n);
unordered_map<char, int> hash_map;
for (int i=0; i<n;i++) {
cin>>input[i];
}
string word;
cin >> word;
for (auto &ch:word) {
hash_map[ch] += 1;
}
int x;
cin >>x;
vector<string> result;
for (int j=0; j<n; j++) {
if(input[j].size()==word.size() && input[j]!=word){
int sum = 0;
unordered_map<char, int> map;
for (auto &ch:input[j]) {
map[ch] += 1;
}
int flag = 0;
for (auto& ch:word) {
if(map[ch]!=hash_map[ch]){
flag = 1;
break;
}
}
if(flag==0){
result.push_back(input[j]);
}
}
}
sort(result.begin(), result.end());
cout<<result.size()<<endl;
if(result.size()!=0){
cout<<result[x-1]<<endl;
}
}
// 64 位输出请用 printf("%lld")
