题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream>
#include <queue>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int check(string s1,string s2)
{
if((s1.size()!=s2.size())||(s1==s2))
{
return 0;
}
for(int i=0;i<s1.size();i++)
{
int pos=s2.find(s1[i]);
if(pos==string::npos)
{
return 0;
}
else {
s2.erase(s2.begin()+pos);
}
}
return 1;
}
int main() {
int n;
while (cin >> n) { // 注意 while 处理多个 case
vector<string> v;
for(int i=0;i<n;i++)
{
string s;
cin>>s;
v.emplace_back(s);
}
string s;
int k;
cin>>s>>k;
int ans=0;
priority_queue<string,vector<string>, greater<string> > bro;
for(int i=0;i<n;i++)
{
if(check(s,v[i]))
{
ans++;
bro.emplace(v[i]);
}
}
cout<<ans<<endl;;
while(!bro.empty())
{
if(k==1)
{
cout<<bro.top()<<endl;
break;
}
else {
k--;
bro.pop();
}
}
}
}
// 64 位输出请用 printf("%lld")
腾讯云智研发成长空间 216人发布