题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream> using namespace std; #include <vector> #include <algorithm> bool isxd(string s1, string s2) { int len1 = s1.length(), len2 = s2.length(); if (len1 != len2 || s1 == s2) { return false; } char c1[len1], c2[len1]; for (int i = 0; i < len1; i++) { c1[i] = s1[i]; c2[i] = s2[i]; } sort(c1, c1 + len1); sort(c2, c2 + len1); int i = 0; for (; i < len1; i++) { if (c1[i] == c2[i]) { continue; } else { break; } } if (i == len1) return true; else return false;; } int main() { int n1, n2; cin >> n1; vector<string> w; for (int i = 0; i < n1; i++) { string temp; cin >> temp; w.insert(w.end(), temp); } string s; cin >> s; cin >> n2; vector<string> xd; for (int i = 0; i < n1; i++) { if (isxd(w[i], s) && isxd(s, w[i])) { xd.insert(xd.end(), w[i]); } } if (xd.size() > 0) { cout << xd.size() << endl; sort(xd.begin(), xd.end()); cout << xd[n2 - 1] << endl; } else { cout<<"0"<<endl; } } // 64 位输出请用 printf("%lld")