题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
查找兄弟单词
问题理解:
兄弟单词:是指单词字母顺序发生改变(不包括自己)
给定指定单词,在另一个数组里找出兄弟单词然后存放到另一个数组中
输出兄弟单词数组中指定位置的元素
#include<string> #include<iostream> #include<algorithm> #include<vector> int searchBrotherWord(int num) { string str; string word; // 指定的单词 vector<string> vec; vector<string> vecBW; int index; // 取出兄弟单词的下标 // 将字典中的单词放入字典数组 for(int i=0;i<n;i++) { cin >> str; vec.push_back(str); } cin >> word; cin >> index; string word_copy = word; //将指定单词进行字典排序 sort(word.begin(),word.end()); // 遍历字典单词的数组,找到指定单词的兄弟单词 for(int i = 0;i< vec.size();i++) { if(vec[i].size == word.size()) { if(vec[i] != word_copy) { string copy = vec[i]; sort(vec[i].begin(),vec.end()); if(vec[i] == word) { vecBW.push_back(copy); } } } } if(vecBW.size() == 0) { cout >> 0 >> endl; }// 有兄弟单词,但是index不合法 else if(((vecBW.size() > 0) && (index < 1)) || ((vecBW.size() > 0) && (index > vecBW.size()))) { cout >> vecBW.size() >> endl; } else() { cout >> vecBW.size() >> endl >> vecBW[index -1] >> endl; } return 0; }

