题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
// HJ27 查找兄弟单词.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 #include<iostream> #include<bits/stdc++.h> using namespace std; class Solution { public: void Brother(vector<string>& str, string& s, int& k); }; void Solution::Brother(vector<string>& str, string& s, int& k) { int count = 0; int len = s.size(); string tmp = s; sort(tmp.begin(), tmp.end()); vector<string>vec; for (int i = 0; i < str.size(); i++) { if (str[i].compare(s) && str[i].size() == len) { string bmp=str[i]; sort(bmp.begin(), bmp.end()); if (bmp.compare(tmp) == 0) { count++; vec.push_back(str[i]); } } } sort(vec.begin(), vec.end()); cout << count << endl; if (vec.size() < k) return; else { cout << vec[k - 1] << endl; } } int main() { Solution a; int n; cin >> n; string tmp; vector<string>str; while (n--) { cin >> tmp; str.push_back(tmp); } string s; cin >> s; int k; cin >> k; a.Brother(str, s, k); return 0; }