题解 | 查找兄弟单词

查找兄弟单词

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

#include <bits/stdc++.h>

using namespace std;

const int N = 1030;

int n, k;
string x, sen;
set<string> se;
vector<string> vc;
bool st[12];

void dfs(int len) {
    int length = x.length();

    if (len == length) {
        // cout << sen << endl;
        if (sen != x) se.insert(sen);
        sen = "";
        return;
    } 
    string SEN = sen;
    for (int i = 0; i < length; i++) {
        if (st[i]) continue;
        st[i] = true;
        sen += x[i];
        dfs(len + 1);
        sen = SEN;
        st[i] = false;
    }
}

int main() {
    cin >> n;

    string str[n];

    for (int i = 0; i < n; i++) cin >> str[i];

    cin >> x;
    cin >> k;

    dfs(0);

    int se_s = se.size();

    for (int i = 0; i < n; i++) {
        if (se.find(str[i]) != se.end() && str[i] != x) vc.push_back(str[i]);
    }

    int vc_s = vc.size();

    if (vc_s == 0) cout << vc_s << endl;
    else {
        sort(vc.begin(), vc.end());
        cout << vc_s << endl;
        cout << vc[k-1] << endl;
    }



    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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