题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n; //数据的输入个数
vector <string> data;
for (int i = 0;i < n;i++)
{
string strs;
cin >> strs;
data.push_back(strs); //接收对应数据个数的容器
}
sort(data.begin(),data.end()); //按照字典排序
string strs_cmp; //接收兄弟单词
cin >> strs_cmp;
string strs_cmp_aft = strs_cmp;
int a; //接收要输出的第几个单词索引
cin >> a;
int geshu = 0; //定义找到兄弟字符串的个数
vector <string> data_after;
for (auto it : data)
{
int shujudaxiao = it.size();
if (shujudaxiao != strs_cmp.size()) //如果长度不相等肯定不是兄弟单词
{
continue;
}
else
{
string fangrushuju = it;
if (it == strs_cmp) //如果完全一样则不是兄弟单词
{
continue;
}
else
{ //看是不是兄弟字符串,如果是就放到新数组中
sort(it.begin(),it.end());
sort(strs_cmp_aft.begin(),strs_cmp_aft.end());
if (it == strs_cmp_aft)
{
data_after.push_back(fangrushuju);
geshu = geshu + 1;
}
}
}
}
cout << geshu << endl;
if (geshu != 0){
cout << data_after[a - 1] << endl;
}
}
// 64 位输出请用 printf("%lld")
阿里云工作强度 694人发布
查看1道真题和解析

