题解 | #查找兄弟单词#

查找兄弟单词

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

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

bool ifBrotherWord(string s,string x)
{
    if(s==x)
        return false;
    if(s.size()!=x.size())
        return false;
    sort(s.begin(),s.end());
    sort(x.begin(),x.end());
    if(s==x)
        return true;
    
    return false;

}

int main() {
    string s;
    getline(cin, s);
    size_t pos = s.find(' ');
    int N = stoi(s.substr(0,pos));
    s = s.substr(pos+1,s.size());
    vector<string> vs(N);
    multiset<string> brother_word;
    for(int i = 0;i!=N;++i)
    {
        pos = s.find(' ');
        string str = s.substr(0,pos);
        vs[i] = str;
        s = s.substr(pos+1,s.size());
    }
    pos = s.find(' ');
    string x = s.substr(0,pos);
    s = s.substr(pos+1,s.size());
    int k = stoi(s);
    for(auto i:vs)
    {
        if(ifBrotherWord(i, x))
            brother_word.emplace(i);
    }

    N = 0;
    cout<<brother_word.size()<<endl;

    for(const auto & i : brother_word)
    {
        if(N==k-1)
        {
            
            cout<<i;
            break;
        }
        else
        {
            ++N;
        }
    }

    return 0;
}
// 64 位输出请用 printf("%lld")

该用桶方法判断的

全部评论

相关推荐

在做核酸的杰尼龟很想去广西嗦粉:bro这个简历看着不错啊,这都找不到吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务