题解 | #查找兄弟单词#

查找兄弟单词

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

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

int main() {
    int n;
    cin >> n;
    vector<string> ss;
    while (n) {
        string t;
        cin >> t;
        ss.push_back(t) ;
        n--;
    }
    string target;
    cin >> target;
    int k;
    cin >> k;

    unordered_map<char, int> thm;
    for (auto c : target) {
        thm[c]++;
    }
    int len = target.length();

    vector<string> res;
    for (auto i : ss) {
        unordered_map<char, int> hm = thm;
        int tlen = 0;
        for (auto c : i) {
            if (hm.find(c) != hm.end() && hm[c] > 0) {
                tlen++;
                hm[c]--;
            } else {
                break;
            }
        }
        if (tlen == len &&i.length()==target.length() && i!=target) {
            res.push_back(i);
        }
    }
    sort(res.begin(), res.end());
    cout << res.size() << endl;
    if(k<res.size())cout << res[k - 1];
    // for(auto s: res)cout<<s<<endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务