题解 | #查找兄弟单词#

查找兄弟单词

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

#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;

static bool isbrother(string word,string brotherword)
{
    multiset<char> wordset;
    multiset<char> brotherwordset;
    bool sign=1;
    if(word==brotherword||word.size()!=brotherword.size())
    {
        sign=0;
    }
    else
    {
        for(auto it:word)
        {
            wordset.insert(it);
        }
        for(auto it:brotherword)
        {
            brotherwordset.insert(it);
        }
        auto itwset=wordset.begin();
        multiset<char>::iterator itbwset=brotherwordset.begin();
        for(;itwset!=wordset.end();)
        {
            if(*itwset!=*itbwset)
            {
                sign=0;
                break;
            }
            itwset++;
            itbwset++;
        }

    }
    return sign;
}

int main()
{
    int n;
    vector<string>arry;
    string word;
    int x;
    vector<string> brothervector;
    string itemp;

    cin>>n;
    while(n--)
    {
        cin>>itemp;
        arry.push_back(itemp);
    }
    cin>>word;
    cin>>x;

    for(int i=0;i<arry.size();i++)
    {
        if(isbrother(word,arry[i]))
        {
            brothervector.push_back(arry[i]);
        }

    }
    sort(brothervector.begin(),brothervector.end());
     cout<<brothervector.size()<<endl;
    if(x<=brothervector.size())
    {
        cout<<brothervector[x-1]<<endl;;
    }
    return 0;

}

全部评论

相关推荐

点赞 评论 收藏
分享
07-18 18:09
门头沟学院 Java
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
06-23 11:28
门头沟学院 Java
牛客91966197...:也有可能是点拒绝的时候自动弹的话术
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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