题解 | 查找兄弟单词

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

bool isBrother(string a, string b)
{
    if (a.length() != b.length() || a == b) {
        return false;
    }
    for (auto ch: a) {
        int idx = b.find(ch);
        if (-1 == idx) {
            return false;
        }
        b.erase(idx, 1);
    }
    return true;
}

int main() {
    int n, idx;
    cin >> n;
    vector<string> strVec;
    string tmp, x;
    for (int i=0; i<n; i++) {
        cin >> tmp;
        strVec.push_back(tmp);
    }
    cin >> x;
    cin >> idx;
    vector<string>::iterator iter = strVec.begin();
    while (iter != strVec.end()) {
        if (!isBrother(*iter, x)) {
            strVec.erase(iter);
        } else {
            ++ iter;
        }
    }
    cout << strVec.size() << endl;
    if (strVec.size() > 0) {
        sort(strVec.begin(), strVec.end());
        cout << strVec[idx-1];
    }

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

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务