题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
bool IsBrothWord(string dit, string world) {
if (dit.length() != world.length() || dit == world)return false;
unordered_map<char, int> dit_map, world_map;
for (int i = 0; i < world.length(); i++) {
dit_map[dit[i]]++;
world_map[world[i]]++;
}
if (dit_map != world_map)return false;
return true;
}
void TM25() {
int n;
cin >> n;
vector<string> dicts;
for (int i = 0; i < n; i++) {
string tempstr;
cin >> tempstr;
dicts.push_back(tempstr);
}
string world;
cin >> world;
int index;
cin >> index;
//输入完成
//查询
vector<string> brothworlds;
for (int i = 0; i < dicts.size(); i++) {
if (IsBrothWord(dicts[i], world)) {
brothworlds.push_back(dicts[i]);
}
}
//排序
sort(brothworlds.begin(), brothworlds.end());
//输出
cout << brothworlds.size() << endl;
if (index <= brothworlds.size()) cout << brothworlds[index - 1] << endl;
}
int main() {
TM25();
}

查看15道真题和解析