题解 | #查找兄弟单词#

查找兄弟单词

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

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

int main() {
    int len;
    cin>>len;
    int count = len;
    vector<string> dict;
    while(count--)
    {
        string temp;
        cin>> temp;
        dict.push_back(temp);
    }
    string target;
    cin>>target;
    int index;
    cin>>index;
    vector<int> hashTar(26,0);
    vector<string>result;
    for(auto i:target)
    {
        hashTar[i-97] ++; 
    }
    for(int i = 0; i < len; i++)
    {
        vector<int> hash(26,0);
        if(dict[i].size()==target.size())
        {
            if(dict[i]!=target)
            {
                for(auto ch:dict[i])
                    hash[ch-97]++;
                bool flag = true;
                for(int j = 0; j < 26;j++)
                {
                    if(hashTar[j]!=hash[j])
                        flag = false;
                }
                if(flag)
                {
                    result.push_back(dict[i]);
                }
            }  
        }
    }
    sort(result.begin(),result.end());
    if(result.size()==0)
        cout<<0;
    else
        cout<<result.size()<<endl<<result[index-1];

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

全部评论

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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