题解 | #查找兄弟单词# 划分为两个部分:判断是否为兄弟单词,将是兄弟单词的按字典序排序
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
bool isBrother(string a, string b)
{
if (a == b)
{
return false;
}
if (a.size() != b.size())
{
return false;
}
else {
vector<int>vec1(26, 0);
for (int i = 0; i < a.size(); i++)
{
vec1[a[i] - 'a']++;
vec1[b[i] - 'a']--;
}
for (int i = 0; i < 26; i++)
{
if (vec1[i] != 0)
{
return false;
}
}
}
return true;
}
int main()
{
int N = 0;
cin >> N;
vector<string>zidian;
for (int i = 0; i < N; i++)
{
string temp;
cin >> temp;
zidian.push_back(temp);
}
string target;
cin >> target;
int m = 0;
cin >> m;
int num = 0;
vector<string>vec;
for (int i = 0; i < zidian.size(); i++)
{
if (isBrother(target, zidian[i]) == true)
{
++num;
vec.push_back(zidian[i]);
}
}
sort(vec.begin(), vec.end());
cout << num << endl;
if (m <= vec.size())
{
cout << vec[m - 1] << endl;
}
return 0;
}
