题解 | #查找兄弟单词#

查找兄弟单词

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

/*
	HW排序3 查找兄弟单词
    
    这题挺离谱的,我刚开始只通过4个测试点,我就调试加了个输出的代码,
    主代码一个没改,就测试了一下就全通过了???
*/
#include <bits/stdc++.h>
using namespace std;

vector<string>allstr;
vector<string>ans;
unordered_map<char,int>hashx;
string x;
int n,k;


bool isbro(int idx,string y)
{
    if(x==y||x.length()!=y.length())return false;
    unordered_map<char,int>hashy;
    for(int i=0;i<y.length();i++){
        if(hashy.find(y[i])!=hashy.end())hashy[y[i]]++;
        else hashy[y[i]]=1;
    }
    //if(hashy['a']==2&&hashy['b']==2)cout<<"第"<<idx<<" "<<y<<" ";
    if(hashx.size()!=hashy.size())return false;
    for(auto it=hashx.begin();it!=hashx.end();it++){
        char c=it->first;
        if(hashx[c]!=hashy[c])return false;
    }
    return true;
}


int main() {
    cin>>n;
    string s;

    for(int i=0;i<n;i++){
        cin>>s;
        allstr.push_back(s);
    }
    cin>>x>>k;
    for(int i=0;i<x.length();i++){
        if(hashx.find(x[i])!=hashx.end())hashx[x[i]]++;
        else hashx[x[i]]=1;
    }


    for(int i=0;i<allstr.size();i++){
        string c=allstr[i];
        if(isbro(i,c))ans.push_back(c);
    }
    stable_sort(ans.begin(),ans.end(),[](string x,string y)->bool{
        return x<y;
    });
    //cout<<endl;
    cout<<ans.size()<<endl;
    if(k<=ans.size())cout<<ans[k-1];

}
// 64 位输出请用 printf("%lld")
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:06
点赞 评论 收藏
分享
06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 14:23
steelhead:你回的有问题,让人感觉你就是来学习的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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