题解 | #查找兄弟单词#

查找兄弟单词

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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;

int main() {
    int n;
    cin >>n;
    vector<string> input(n);
    unordered_map<char, int> hash_map;
    for (int i=0; i<n;i++) {
        cin>>input[i];
    }
    string word;
    cin >> word;
    for (auto &ch:word) {
        hash_map[ch] += 1;
    }
    int x;
    cin >>x;

    vector<string> result;
    for (int j=0; j<n; j++) {
        if(input[j].size()==word.size() && input[j]!=word){
            int sum = 0;
            unordered_map<char, int> map;
            for (auto &ch:input[j]) {
                map[ch] += 1;
            }
            int flag = 0;
            for (auto& ch:word) {
                if(map[ch]!=hash_map[ch]){
                    flag = 1;
                    break;
                }
            }
            if(flag==0){
                result.push_back(input[j]);
            }
            
        }
    }

    sort(result.begin(), result.end());
    cout<<result.size()<<endl;
    if(result.size()!=0){
        cout<<result[x-1]<<endl;
    }
    
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

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

创作者周榜

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