题解 | #查找兄弟单词#

查找兄弟单词

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

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

int main(void)
{
    int ans = 0;
    vector<string> words;
    int n;
    cin >> n;
    vector<string> strs(n);
    string target;
    int k;
    
    for (int i = 0; i < n; i++)
    {
        cin >> strs[i];
    }
    cin >> target >> k;
    string tmp1 = target;
    sort(tmp1.begin(),tmp1.end());
    for (int i = 0; i < n; i++)
    {
        //不满足兄弟单词的规则就跳过
        if (strs[i].size() != target.size() || strs[i] == target)
        {
            continue;
        }
        string tmp = strs[i];
        sort(tmp.begin(),tmp.end());
        if (tmp == tmp1)
        {
            ans++;//更新兄弟单词个数
            words.push_back(strs[i]);//更新兄弟单词
        }
    }
    sort(words.begin(),words.end());//按字典排序兄弟单词
    cout << ans << endl;
    if (!words.empty() && k < words.size())//防止越界
    {
        cout << words[k-1] << endl;
    }
    return 0; 
}
全部评论

相关推荐

牛客41406533...:回答他在课上学,一辈子待在学校的老教授用三十年前的祖传PPT一字一句的讲解,使用谭浩强红皮书作为教材在devc++里面敲出a+++++a的瞬间爆出114514个编译错误来学这样才显得专业
点赞 评论 收藏
分享
想玩飞盘的菠萝蜜在春...:上交✌🏻也拒?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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