sacgajcvs

Untitled

Mar 20th, 2022
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string ltrim(const string &);
  6. string rtrim(const string &);
  7.  
  8.  
  9.  
  10. /*
  11. * Complete the 'sortBoxes' function below.
  12. *
  13. * The function is expected to return a STRING_ARRAY.
  14. * The function accepts STRING_ARRAY boxList as parameter.
  15. */
  16.  
  17. vector<string> sortBoxes(vector<string> boxList) {
  18. int n = boxList.size();
  19. vector<pair<vector<string>, int>> sorted_list;
  20. auto fun = [&] (string temp) {
  21. int ps = -1;
  22. for(int i = 0; i < temp.length(); i++) {
  23. if(temp[i] == ' ') {
  24. ps = i;
  25. break;
  26. }
  27. }
  28. return make_pair(temp.substr(0, ps), temp.substr(ps + 1, temp.length() - ps - 1));
  29. };
  30. auto check_new = [&] (string temp) {
  31. return (temp[0] = '0' && temp[0] >= '9');
  32. };
  33. auto increment_num = [&] (string& num) {
  34. int c = 1, cur = 0;
  35. string s = "";
  36. for(int i = num.length() - 1; i >= 0 ; i--) {
  37. cur = (num[i] - '0');
  38. cur += c;
  39. if(cur > 9) {
  40. c = 1;
  41. cur = cur % 10;
  42. } else {
  43. c = 0;
  44. }
  45. s += (char)(cur + '0');
  46. }
  47. reverse(s.begin(), s.end());
  48. num = s;
  49. };
  50. string num = "0000";
  51. for(int i = 0; i < n; i++) {
  52. pair<string, string> out = fun(boxList[i]);
  53. cout << out.first << " " << out.second[0] << endl;
  54.  
  55. if(check_new(out.second)) {
  56. sorted_list.push_back({{"b", num}, i});
  57. increment_num(num);
  58. } else {
  59. sorted_list.push_back({{"a", out.second, out.first}, i});
  60. }
  61. }
  62. sort(sorted_list.begin(), sorted_list.end());
  63. vector<string> final_out;
  64. for(int i = 0; i < n; i++) {
  65. final_out.push_back(boxList[sorted_list[i].second]);
  66. }
  67. return final_out;
  68. }
  69.  
  70. int main()
  71. {
  72. // ofstream cout(getenv("OUTPUT_PATH"));
  73.  
  74. string boxList_count_temp;
  75. getline(cin, boxList_count_temp);
  76.  
  77. int boxList_count = stoi(ltrim(rtrim(boxList_count_temp)));
  78.  
  79. vector<string> boxList(boxList_count);
  80.  
  81. for (int i = 0; i < boxList_count; i++) {
  82. string boxList_item;
  83. getline(cin, boxList_item);
  84.  
  85. boxList[i] = boxList_item;
  86. }
  87.  
  88. vector<string> result = sortBoxes(boxList);
  89.  
  90. for (int i = 0; i < result.size(); i++) {
  91. cout << result[i];
  92.  
  93. if (i != result.size() - 1) {
  94. cout << "\n";
  95. }
  96. }
  97.  
  98. cout << "\n";
  99.  
  100.  
  101. return 0;
  102. }
  103.  
  104. string ltrim(const string &str) {
  105. string s(str);
  106.  
  107. s.erase(
  108. s.begin(),
  109. find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
  110. );
  111.  
  112. return s;
  113. }
  114.  
  115. string rtrim(const string &str) {
  116. string s(str);
  117.  
  118. s.erase(
  119. find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
  120. s.end()
  121. );
  122.  
  123. return s;
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment