题解 | #查找兄弟单词#

查找兄弟单词

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

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

bool func(string &s, string &t){
    if (s.size() != t.size() || s == t) return false;
    unordered_map<char, int> mp;
    for (int i = 0; t[i]; i++){
        mp[t[i]] += 1;
    }
    for (int i = 0; s[i]; i++){
        if (mp.find(s[i]) == mp.end()) return false;
        mp[s[i]]--;
        if (mp[s[i]] < 0) return false;
    }
    return true;
}

int main(){
    int n;
    while (cin >> n){
        vector<string> s(n);
        for (int i = 0; i < n; i++){
            cin >> s[i];
        }
        string b;
        cin >> b;
        int k = 0;
        cin >> k;
        vector<string> v;
        for (int i = 0; i < n; i++){
            if (func(s[i], b)) v.push_back(s[i]);
        }
        sort(v.begin(), v.end());
        cout << v.size() << endl;
        if (k < v.size()){
            cout << v[k-1] << endl;
        }
    }
    return 0;
}
全部评论

相关推荐

头像
不愿透露姓名的神秘牛友
04-29 12:10
点赞 评论 收藏
转发
1 收藏 评论
分享
牛客网
牛客企业服务