题解 | #查找兄弟单词#
查找兄弟单词
https://www.nowcoder.com/practice/03ba8aeeef73400ca7a37a5f3370fe68
#include <iostream> #include<string> #include<vector> #include<algorithm> using namespace std; bool cmp(string a, string b) { return a + b < b + a; } int main() { vector<string> word; vector<string> newword; string ss; string words; //原输入 string words1; // 排序之后的 int num; int count = 0; int size = 0; cin >> num; for (int i = 0; i < num; i++) { cin >> ss; word.push_back(ss); } cin >> words; cin >> size; //step1 按照字典进行排序 sort(word.begin(), word.end(), cmp); //step2 对输入的每个元素进行排序 vector<string>::iterator it = word.begin(); //对words进行排序 words1 = words; sort(words1.begin(), words1.end()); for (; it != word.end(); it++) { if ((*it).compare(words) == 0) { // 如果单词没排序相同,过滤掉 continue; } string str = *it; sort((*it).begin(), (*it).end()); if ((*it).compare(words1) == 0) { count++; newword.push_back(str); } } cout << count << endl; for (int i = 0; i < newword.size(); i++) { if (size - 1 == i) { cout << newword[i]; break; } } return 0; }