题解 | #查找兄弟单词#

查找兄弟单词

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//判断两个字符串是否为兄弟
bool isBrother(string s1, string s2) {
    if (s1.length() == s2.length()) {
        if (s1 == s2)
            return false;
        sort(s1.begin(), s1.end());
        sort(s2.begin(), s2.end());
        if (s1 == s2)
            return true;
    }
    return false;
}
int main() {
    int n;
    while (cin >> n) {
        string str;
        vector<string> strs;
	  //输入字符串
        for (int i = 0; i < n; i++) {
            cin >> str;
            strs.push_back(str);
        }
        cin >> str;
        int k;
        cin >> k;
        vector<string> ans;
        for (int i = 0; i < strs.size(); i++) {
            if (strs[i] == str)
                continue;
            if (isBrother(str, strs[i])) {
                ans.push_back(strs[i]);
            }
        }
        sort(ans.begin(), ans.end());
        cout << ans.size() << endl;
        if (k > 0 && k < ans.size())
            cout << ans[k - 1] << endl;
    }
    return 0;

}

全部评论

相关推荐

炫哥_:为什么都读硕士了?项目还是网上的项目(真心发问)
最后再改一次简历
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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