题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; //判断两个单词是否为兄弟单词 bool is_brother(string word_1,string word_2){ //如果两个单词相同则不是兄弟单词 if(word_1 == word_2){ return false; } //两个单词不同,但是含有相同的字符则为兄弟单词 sort(word_1.begin(), word_1.end()); sort(word_2.begin(),word_2.end()); return word_1==word_2; } //获取全部兄弟单词 vector<string> brother_words(vector<string> words,string x){ vector<string>brother_words; for(int i=0;i<words.size();++i){ if(is_brother(words[i], x)){ brother_words.push_back(words[i]); } } //将兄弟单词按字典序排列 sort(brother_words.begin(),brother_words.end()); return brother_words; } int main() { int n,k; string x,word; vector<string>bro_w; vector<string>words; cin>>n; while (n>0) { cin>>word; words.push_back(word); --n; } cin >> x; cin >> k; bro_w = brother_words(words,x); if(k<bro_w.size()){ cout<<bro_w.size()<<endl; cout<<bro_w[k-1]; }else{ cout<<bro_w.size()<<endl; } return 0; }