题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream> #include <map> #include <vector> using namespace std; int main() { int n, res = 0; cin >> n; vector<string> dict; for (int i = 0; i < n; i++) { string tmp; cin >> tmp; dict.push_back(tmp); } string tar; cin >> tar; int tar_len = tar.size(); int k; cin >> k; map<string, int> mp; vector<int> bro(26, 0); for (int i = 0; i < tar_len; i++) { bro[static_cast<int>(tar[i] - 'a')]++; } for (int i = 0; i < n; i++) { bool fa = true; string tmp = dict[i]; vector<int> tm(26, 0); int lens = tmp.size(); for (int j = 0; j < lens; j++) { tm[tmp[j] - 'a']++; } for (int j = 0; j < 26; j++) { if (bro[j] != tm[j]) { fa = false; } } // cout << fa << endl; if (fa && tar != tmp) { res++; if (mp.find(tmp) == mp.end()) { mp[tmp] = 1; } else { mp[tmp]++; } } } cout << res << endl; for (const auto& j : mp) { if (k > j.second) { k -= j.second; } else { cout << j.first << endl; break; } } return 0; } // 64 位输出请用 printf("%lld")
和查找相关的题目一般都会考虑哈希表。