题解 | #查找兄弟单词#

查找兄弟单词

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

#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

bool isBrother(string a, string b) {
    if (a != b) {
        sort(a.begin(), a.end());
        sort(b.begin(), b.end());
        return a == b;
    } else {
        return false;
    }
}

int main() {
    int n, k;
    string x;
    vector<string> strs;
    multiset<string> Set;

    cin >> n;
    while (n--) { 
        string str;
        cin >> str;
        strs.push_back(str);
    }

    cin >> x;
    cin >> k;

    for (string s : strs)
        if (isBrother(s, x))
            Set.insert(s);

    cout << Set.size() << endl;
    if (k >= 1 && k <= Set.size()) {
        set<string>::iterator iter = Set.begin();
        while (--k)
            iter++;
        cout << *iter << endl;
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 17:30
点赞 评论 收藏
分享
VirtualBoo...:都去逗他了?
点赞 评论 收藏
分享
06-11 13:34
门头沟学院 C++
offe从四面八方来:我真的没时间陪你闹了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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