题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
比较常见的一道题
关键在于怎么判断两个字符串是否是兄弟串
用数组来做统计比较合适
#include #include #include #include using namespace std; bool isBrother(string t, string s); int main(){ int n; cin >> n; vector vs(n); for(int i=0; i<n; i++){ cin >> vs[i]; } string targetS; cin >> targetS; int k; cin >> k; vector BrotherVector; for(auto x:vs){ if(isBrother(targetS, x)){ BrotherVector.push_back(x); } } sort(BrotherVector.begin(), BrotherVector.end()); cout << BrotherVector.size() << endl; if(k-1<BrotherVector.size()){ cout << BrotherVector[k-1] << endl; } return 0; } bool isBrother(string t, string s){ if(t==s) return false; vector vt(26, 0); vector vs(26, 0); for(auto x:t){ vt[x-'a']++; } for(auto x:s){ vs[x-'a']++; } return vt==vs; }