题解 | #查找兄弟单词#

查找兄弟单词

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

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

bool is_brothers(string a,string b){
    if(a==b)return false;
    int a_size=a.size(),b_size=b.size();
    if(a_size!=b_size)return false;
    vector<int>target_ump(26,0);

    for(char ch:a)
        target_ump[ch-'a']++;
    for(char ch:b)
    {
        if(target_ump[ch-'a']==0)return false;
        else 
        target_ump[ch-'a']--;
    }

    for(int x:target_ump)
        if(x!=0)return false;

    return true;
}

int main(){
    int N=0;

    while(cin>>N){
        list<string> str_list;
        int k=0;
        string str="",target="";
        for(int i=0;i<N;i++){
            cin>>str;
            str_list.push_back(str);
        }
        cin>>target>>k;

        vector<string> result;
        for(auto itr=str_list.begin();itr!=str_list.end();itr++){
            if(is_brothers(target, *itr))
                result.push_back(*itr);
        }
        stable_sort(result.begin(), result.end());
        cout<<result.size()<<endl;
        if(k<result.size())cout<<result[k-1]<<endl;
        return 0;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 16:22
点赞 评论 收藏
分享
牛客84809583...:举报了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-03 17:37
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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