华为机试题 HJ27题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <cctype>
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
bool isBotherWord(const string &source, const string &dest)
{
if (source.length() != dest.length() || source == dest) {
return false;
}
string s1 = source;
//std::transform(s1.begin(), s1.end(), s1.begin(), tolower);
std::sort(s1.begin(), s1.end());
string s2 = dest;
//std::transform(s2.begin(), s1.end(), s1.begin(), tolower);
std::sort(s2.begin(), s2.end());
return s1 == s2;
}
int main() {
int n, k;
vector<string> sVec;
string x;
while (cin >> n) { // 注意 while 处理多个 case
// 输入n个单词作为字典单词
string sTemp;
for (int i = 0; i < n; i++) {
cin >> sTemp;
sVec.push_back(sTemp);
}
cin >> x;
cin >> k;
vector<string> botherWords;
for (auto word : sVec) {
if (isBotherWord(x, word)) {
botherWords.push_back(word);
}
}
// 对botherWords按照字典顺序进行排序
std::sort(botherWords.begin(), botherWords.end());
cout << botherWords.size() << endl;
if ((int)(botherWords.size()) > 0 && k <= (int)(botherWords.size()) - 1) {
cout << botherWords[k-1];
}
sVec.clear();
}
}
// 64 位输出请用 printf("%lld")


