题解 | #查找兄弟单词#

查找兄弟单词

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;

}

全部评论

相关推荐

02-07 10:52
复旦大学 Java
混子不想混:非常能理解,感觉他们就靠着入行早,打压新人一样。我这个公司也是,天天干的累死累活,然后绩效打C,合着让新人被绩效,像是年底攒棺材本一样。总是打击之后,还会让人开始自我怀疑,是不是我努力的还不够,实际上并不是,就是他们不做人,故意打压新人。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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