题解 | #查找兄弟单词#

查找兄弟单词

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

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// 构造布尔函数,判断word是否是x的兄弟单词
bool isBrotherWord(string word,string x){
    if (word == x){
        return false;
    }
    sort(x.begin(),x.end());
    sort(word.begin(),word.end());
    if (x == word){
        return true;
    }
    else return false;
}

int main() {
    int n;
    // 输入字典中单词的个数n
    cin >> n;
    string word;
    // 输入n个单词作为字典单词
    vector<string> vec;
    while (n--) { 
        cin >> word;
        vec.push_back(word);
    }
    // 寻找 x 的兄弟单词里,按字典序排列后的第 k 个单词是什么?
    string x;
    cin >> x;
    int k;
    cin >> k;
    // res为存储x的兄弟单词的数组
    vector<string> res;
    for (auto i:vec){
        if (isBrotherWord(i, x)){
            res.push_back(i);
        }
    }
    int m;
    m = res.size();
    cout << m << endl;
    // 对res进行字典排序
    sort(res.begin(),res.end());
    if (m >= k){
        cout << res[k-1] << endl;
    }
    return 0;
}

#23届找工作求助阵地##零基础学习C++##14天打卡计划#
全部评论

相关推荐

mq2:我倒是觉得这种敞亮一点好。能接受就去不能就不去呗。 完了跟现在“正常”公司一样,hr说的天花乱坠,进去一看根本就是996核动力牛马,想走又没应届生身份了。岂不是更糟。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务