题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream> #include <map> #include <vector> using namespace std; map<string,int> mp; void genOther(string str,int begin){ if (begin==str.size()) return; for (int i = begin; i < str.size(); ++i) { swap(str[i],str[begin]); mp[str] = 0; genOther(str,begin+1); swap(str[i],str[begin]); } } int main(){ int n,k; string x; while (cin>>n){ vector<string> wv(n); for (int i = 0; i < n; ++i) { cin>>wv[i]; } cin>>x>>k; genOther(x,0); int count = 0; for (int i = 0; i < wv.size(); ++i) { if (wv[i].size() != x.size()) continue; if (wv[i]!=x && mp.find(wv[i]) != mp.end()){ mp[wv[i]]++; count++; } } string res; for(auto it : mp){ while(it.second--){ k--; if (k==0){ res = it.first; break; } } } cout<<count<<endl; if (!res.empty()) cout<<res<<endl; } return 0; }