千寻の: 自定义比较函数和优先级 #include <iostream> #include <string> #include <vector> #include <algorithm> #include<sstream> using namespace std; //自定义比较函数,自定义优先级 bool StringCompare(string& a, string& b) { const string priority = "$&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; for (int i = 0; i < a.size(); i++) { auto pos_a = priority.find(a[i]); auto pos_b = priority.find(b[i]); if (pos_a != pos_b) { return pos_a < pos_b;//确保从编号开始比较,按$&*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ排序 } } return a < b;//按题目意思,其实不可重复,完全用不到的 } int main() { string s; cin >> s; stringstream ss(s); vector<string> flights; string x; while (getline(ss, x, ',')) { flights.push_back(x); } sort(flights.begin(), flights.end(), StringCompare); cout << flights.front(); flights.erase(flights.begin()); for (const auto& flight : flights) { cout << "," << flight; } cout << endl; return 0; }
华为校招机试 - 字符串解析(20240515)
华为OD机试 - 机场航班调度程序(Java & JS & Python & C & C++)