题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <cstring> #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; bool BrotherWord(string wd_1, string wd_2){ if(wd_1.length() != wd_2.length()) return false; else if(wd_1 == wd_2) return false; else{ sort(wd_1.begin(), wd_1.end()); sort(wd_2.begin(), wd_2.end()); if(wd_1 == wd_2) return true; else return false; } } int main() { int num_cmp_str, queue_brother; int num_brother = 0; cin >> num_cmp_str; vector<string> vec_1; vector<string> vec_2; for(int i = 0; i< num_cmp_str; i++){ string str; cin >> str; vec_1.push_back(str); } string baseString; cin >> baseString >> queue_brother; for(string it : vec_1){ if(BrotherWord(baseString, it)){ num_brother ++; vec_2.push_back(it); } } sort(vec_2.begin(), vec_2.end()); cout << num_brother << endl; if(!vec_2.empty()) cout << vec_2[queue_brother-1] << endl;//第一次提交时没有考虑vec_2为空的情况,导致发生了段错误 return 0; }