题解 | #查找兄弟单词#
查找兄弟单词
http://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
一个比较容易想的思路,时间70%
// 思路:使用multi_set集合存放输入,升序排列, 同时不应去重
// 将集合中符合兄弟单词的字符串 存入数组vec,
// 返回数组size 以及 vec[k-1]
#include <bits/stdc++.h>
using namespace std;
void func()
{
int num;
cin>>num; // 输入字典数量
string str;
multiset<string> s1;
while(num>0 && cin>>str)
{
s1.insert(str); // 保存字典
num--;
}
cin>> str; // 目标字符串
cin>> num; // k
string str_0 = str;
vector<string> res;
sort(str.begin(),str.end());
for(auto s:s1){ // 判断是否为兄弟:原型不同,排序后相同
if(s!=str_0){
string temp =s;
sort(temp.begin(),temp.end());
if(temp==str)
res.push_back(s);
}
}
if(res.size()>0)
cout<< res.size()<<endl<<res[num-1];
else cout<<0; // 孤儿
}
int main()
{
func();
}