题解 | #查找兄弟单词#

查找兄弟单词

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

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

bool cmp(string a, string b) {
    return a + b < b + a;
}

int main() {
    vector<string> word;
    vector<string> newword;
    string ss;
    string words; //原输入
    string words1; // 排序之后的
    int num;
    int count = 0;
    int size = 0;
    cin >> num;

    for (int i = 0; i < num; i++) {
        cin >> ss;
        word.push_back(ss);
    }

    cin >> words;
    cin >> size;

    //step1 按照字典进行排序
    sort(word.begin(), word.end(), cmp);

    //step2 对输入的每个元素进行排序
    vector<string>::iterator it = word.begin();

    //对words进行排序
    words1 = words;
    sort(words1.begin(), words1.end());


    for (; it != word.end(); it++) {
        if ((*it).compare(words) == 0) { // 如果单词没排序相同,过滤掉
            continue;
        }
        string str = *it;
        sort((*it).begin(), (*it).end());
        if ((*it).compare(words1) == 0) {
            count++;
            newword.push_back(str);

        }
    }

    cout << count << endl;


    for (int i = 0; i < newword.size(); i++) {
        if (size - 1 == i) {
            cout << newword[i];
            break;
        }
    }


    return 0;
}

全部评论

相关推荐

野猪不是猪🐗:😇:恭喜你以出色的表现成为xxx的一员 😨:您以进入本公司人才库 实际点开:您愿望单中的xxx正在特卖!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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