题解 | #查找兄弟单词#

查找兄弟单词

https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
bool isBrother(string s1,string s2){
    if(s1.size()==s2.size()){
        if(s1==s2){
            return false;
        }
        sort(s1.begin(),s1.end());
        sort(s2.begin(),s2.end());
        if(s1==s2){
            return true;
        }
    }
    return false;
}
int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        vector<string>str;
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            str.push_back(s);
        }
        string ss;
        cin>>ss;
        int k;
        cin>>k;
        vector<string>ans;
        for(int i=0;i<n;i++){
            if(isBrother(ss,str[i])){
                ans.push_back(str[i]);
            }
        }
        sort(ans.begin(),ans.end());
        cout<<ans.size()<<endl;
        if(ans.size()>=k){
            cout<<ans[k-1]<<endl;
        }
        
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

葬爱~冷少:我当时都是上午刷力扣,下午背八股,有活给我先别急,没活就干自己的事情
点赞 评论 收藏
分享
头像
04-17 09:29
已编辑
湖南农业大学 后端
睡姿决定发型丫:本硕末9也是0offer,简历挂了挺多,只有淘天 美团 中兴给了面试机会,淘天二面挂,美团一面kpi面,中兴一面感觉也大概率kpi(虽然国企,但一面0技术纯聊天有点离谱吧)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务