题解 | 查找兄弟单词

查找兄弟单词

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

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

int main() {
    int n, k;
    cin >> n;
    vector<string> s(n);
    for(int i = 0; i < n; ++i) {
        cin >> s[i];
    }
    string x;
    cin >> x;
    cin >> k;
    if(x.size() < 2) {
        return 0;
    }
    int bro_num = 0;
    vector<string> bro_str;
    for(const string& str: s) {
        if(str.size() != x.size() || str == x) {
            continue;
        }
        string tmp1 = str, tmp2 = x; //不能直接修改str和x  不然后续循环开始处if str == x要跳过的
        sort(tmp1.begin(), tmp1.end());
        sort(tmp2.begin(), tmp2.end());
        if(tmp1 == tmp2) {
            ++bro_num;
            bro_str.push_back(str);
        }
        
    }
    sort(bro_str.begin(), bro_str.end());
    cout << bro_num << endl;
    if(k > bro_num) return 0;
    cout << bro_str[k-1] << endl;
    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务