题解 | #查找兄弟单词#
查找兄弟单词
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; }