题解 | #查找兄弟单词#

查找兄弟单词

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

#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool BrotherWord(string wd_1, string wd_2){
    if(wd_1.length() != wd_2.length()) return false;
    else if(wd_1 == wd_2) return false;
    else{
        sort(wd_1.begin(), wd_1.end());
        sort(wd_2.begin(), wd_2.end());
        if(wd_1 == wd_2) return true;
        else return false;
    }
}

int main() {
    int num_cmp_str, queue_brother;
    int num_brother = 0;
    cin >> num_cmp_str;
    vector<string> vec_1;
    vector<string> vec_2;
    for(int i = 0; i< num_cmp_str; i++){
        string str;
        cin >> str;
        vec_1.push_back(str);
    }
    string baseString;
    cin >> baseString >> queue_brother;

    for(string it : vec_1){
        if(BrotherWord(baseString, it)){
            num_brother ++;
            vec_2.push_back(it);
        }
    }
    sort(vec_2.begin(), vec_2.end());

    cout << num_brother << endl;
    if(!vec_2.empty()) cout << vec_2[queue_brother-1] << endl;//第一次提交时没有考虑vec_2为空的情况,导致发生了段错误
    return 0;
}

全部评论

相关推荐

每晚夜里独自颤抖:把华北改为华南再试一试,应该就没啥问题了。改完可能都不用投,别人主动联系了。
点赞 评论 收藏
分享
qq乃乃好喝到咩噗茶:院校后面加上211标签,放大加粗,招呼语也写上211
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务