题解 | #查找兄弟单词#

查找兄弟单词

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

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <algorithm>


void findandgetword(int n , int k, std::string& x, std::vector<std::string>& wordsvec) {
    std::map<char, int> map;
    for (char i: x) {
        map[i] ++;
    }
    int nums = 0;
    std::vector<std::string> okvec;
    //遍历每个单词
    for (int i = 0; i < n; i++) {
        if (wordsvec[i] == x) continue;
        std::map<char, int> tempmap(map);
        bool flag = true;
        //遍历每个单词的字母
        for (int j = 0; j < wordsvec[i].size(); j++) {
            
            if (tempmap.find(wordsvec[i][j]) == tempmap.end()) {
                flag = false;
            }
            else {
                tempmap[wordsvec[i][j]] --;
            }
        }
        for (auto tempchar : tempmap) {
            if (tempchar.second != 0) {
                flag = false;
            }
        }
        if (flag) {
            okvec.push_back(wordsvec[i]);
            nums ++;
        }

    }
    std::cout << nums << std::endl;
    std::sort(okvec.begin(), okvec.end());
    if (k <= okvec.size()) {
        std::cout << okvec[k - 1];
    }

}

int main() {
    std::string s;
    std::getline(std::cin ,s);
    std::stringstream ss(s);
    std::string token;
    std::vector<std::string> strvec;
    while (std::getline(ss, token, ' ')) {
        strvec.push_back(token);
    }
    int n = std::stoi(strvec[0]);
    int k = std::stoi(strvec[strvec.size() - 1]);
    std::string x = strvec[strvec.size() - 2];
    std::vector<std::string> wordsvec(strvec.begin() + 1, strvec.end() - 2);
    findandgetword(n, k, x, wordsvec);
}

全部评论

相关推荐

owwhy:难,技术栈在嵌入式这块显得非常浅,并且简历有大问题。教育经历浓缩成两行就行了,写什么主修课程,说的不好听这块没人在意,自我评价删了,项目写详细点,最终简历缩成一页。相关技能怎么说呢,有点差了,还写成这么多行
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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