题解 | #查找兄弟单词#

查找兄弟单词

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")

全部评论

相关推荐

07-15 11:41
门头沟学院 Java
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
07-14 12:29
门头沟学院 Java
后端岗,实习三周感觉有点想跑路了,担心秋招被拉黑,有没有佬是字节HR知道情况的
从零开始的转码生活:你实习三周都想跑路,将来拿到offer真的愿意在这干十几二十年吗
投递字节跳动等公司8个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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