Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string ltrim(const string &);
- string rtrim(const string &);
- /*
- * Complete the 'sortBoxes' function below.
- *
- * The function is expected to return a STRING_ARRAY.
- * The function accepts STRING_ARRAY boxList as parameter.
- */
- vector<string> sortBoxes(vector<string> boxList) {
- int n = boxList.size();
- vector<pair<vector<string>, int>> sorted_list;
- auto fun = [&] (string temp) {
- int ps = -1;
- for(int i = 0; i < temp.length(); i++) {
- if(temp[i] == ' ') {
- ps = i;
- break;
- }
- }
- return make_pair(temp.substr(0, ps), temp.substr(ps + 1, temp.length() - ps - 1));
- };
- auto check_new = [&] (string temp) {
- return (temp[0] = '0' && temp[0] >= '9');
- };
- auto increment_num = [&] (string& num) {
- int c = 1, cur = 0;
- string s = "";
- for(int i = num.length() - 1; i >= 0 ; i--) {
- cur = (num[i] - '0');
- cur += c;
- if(cur > 9) {
- c = 1;
- cur = cur % 10;
- } else {
- c = 0;
- }
- s += (char)(cur + '0');
- }
- reverse(s.begin(), s.end());
- num = s;
- };
- string num = "0000";
- for(int i = 0; i < n; i++) {
- pair<string, string> out = fun(boxList[i]);
- cout << out.first << " " << out.second[0] << endl;
- if(check_new(out.second)) {
- sorted_list.push_back({{"b", num}, i});
- increment_num(num);
- } else {
- sorted_list.push_back({{"a", out.second, out.first}, i});
- }
- }
- sort(sorted_list.begin(), sorted_list.end());
- vector<string> final_out;
- for(int i = 0; i < n; i++) {
- final_out.push_back(boxList[sorted_list[i].second]);
- }
- return final_out;
- }
- int main()
- {
- // ofstream cout(getenv("OUTPUT_PATH"));
- string boxList_count_temp;
- getline(cin, boxList_count_temp);
- int boxList_count = stoi(ltrim(rtrim(boxList_count_temp)));
- vector<string> boxList(boxList_count);
- for (int i = 0; i < boxList_count; i++) {
- string boxList_item;
- getline(cin, boxList_item);
- boxList[i] = boxList_item;
- }
- vector<string> result = sortBoxes(boxList);
- for (int i = 0; i < result.size(); i++) {
- cout << result[i];
- if (i != result.size() - 1) {
- cout << "\n";
- }
- }
- cout << "\n";
- return 0;
- }
- string ltrim(const string &str) {
- string s(str);
- s.erase(
- s.begin(),
- find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
- );
- return s;
- }
- string rtrim(const string &str) {
- string s(str);
- s.erase(
- find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
- s.end()
- );
- return s;
- }
Advertisement
Add Comment
Please, Sign In to add comment