fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // Định nghĩa cấu trúc Node cho linked list
  5. struct Node {
  6. std::string code;
  7. int count;
  8. Node* next;
  9.  
  10. Node(const std::string& c) : code(c), count(1), next(nullptr) {}
  11. };
  12.  
  13. // Hàm chèn một node mới vào linked list đã sắp xếp theo mã hàng
  14. void insertSorted(Node*& head, const std::string& code) {
  15. Node* newNode = new Node(code);
  16. if (!head || code < head->code) {
  17. newNode->next = head;
  18. head = newNode;
  19. } else {
  20. Node* current = head;
  21. while (current->next && current->next->code < code) {
  22. current = current->next;
  23. }
  24. if (current->next && current->next->code == code) {
  25. current->next->count++;
  26. delete newNode;
  27. } else {
  28. newNode->next = current->next;
  29. current->next = newNode;
  30. }
  31. }
  32. }
  33.  
  34. // Hàm chuyển linked list sang mảng
  35. void linkedListToArray(Node* head, std::pair<std::string, int> arr[], int& size) {
  36. size = 0;
  37. Node* current = head;
  38. while (current) {
  39. arr[size++] = {current->code, current->count};
  40. current = current->next;
  41. }
  42. }
  43.  
  44. bool slt(std::string x, std::string y) {
  45. if (x.size() < y.size()) return true;
  46. else if (x.size() > y.size()) return false;
  47. return (x < y);
  48. }
  49.  
  50. int partition(std::pair<std::string, int> a[], int left, int right) {
  51. std::pair<std::string, int> pivot = a[right];
  52. int id = left - 1;
  53. for (int i = left; i < right; i++) {
  54. if ((a[i].second > pivot.second) || (a[i].second == pivot.second && slt(a[i].first, pivot.first))) {
  55. id++;
  56. std::swap(a[id], a[i]);
  57. }
  58. }
  59. id++;
  60. std::swap(a[id], a[right]);
  61. return id;
  62. }
  63.  
  64. void Sort_pairSecond(std::pair<std::string, int> a[], int left, int right) {
  65. if (left < right) {
  66. int id_pivot = partition(a, left, right);
  67. Sort_pairSecond(a, left, id_pivot - 1);
  68. Sort_pairSecond(a, id_pivot + 1, right);
  69. }
  70. }
  71.  
  72. int main() {
  73. int n;
  74. std::cin >> n;
  75. std::cin.ignore();
  76.  
  77. Node* head = nullptr;
  78. for (int i = 0; i < n; ++i) {
  79. std::string code;
  80. std::cin >> code;
  81. insertSorted(head, code);
  82. }
  83.  
  84. std::pair<std::string, int> arr[n]; // Kích thước tối đa
  85. int size = 0;
  86. linkedListToArray(head, arr, size);
  87.  
  88. Sort_pairSecond(arr, 0, size - 1);
  89.  
  90. for (int i = 0; i < size; ++i) {
  91. std::cout << arr[i].first << ' ' << arr[i].second << '\n';
  92. }
  93.  
  94. // Giải phóng bộ nhớ của linked list
  95. Node* current = head;
  96. while (current) {
  97. Node* next = current->next;
  98. delete current;
  99. current = next;
  100. }
  101.  
  102. return 0;
  103. }
Success #stdin #stdout 0.01s 5288KB
stdin
10

123

123444

123444

12

1

8

1

455

455

9
stdout
455 2
123444 2
1 1
1 1
8 1
9 1
12 1
123 1