题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n, k; cin >> n; vector<string> word; vector<string> broword; string oldword; string m1temp; for(int i = 1; i <= n ; i++){ cin >> m1temp; word.push_back(m1temp); } cin >> oldword >> k; for(int i = 1; i <= n; i++){ if(word.back().length() != oldword.length()){ word.pop_back(); continue; } if(word.back() == oldword){ word.pop_back(); continue; } m1temp = word.back(); string m2temp = oldword; sort(m1temp.begin(), m1temp.end()); sort(m2temp.begin(), m2temp.end()); if(m1temp == m2temp){ broword.push_back(word.back()); } word.pop_back(); } cout << broword.size() << endl; if(broword.size() == 0) return 0; sort(broword.begin(), broword.end()); cout << broword[k - 1]; }
这题要注意指针越界,边界条件要细心