-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmstrings.cpp
96 lines (89 loc) · 2.56 KB
/
mstrings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string.h>
#include <algorithm>
#include <vector>
#include <math.h>
#include <iomanip>
#include <stack>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
#define all(v) v.begin(), v.rend()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
void quickSort(vector<int> &vec, int l, int r)
{
int i = l;
int j = r;
int m = vec[(i + j) / 2];
while (i <= j){
while (vec[i] < m) i++;
while (vec[j] > m) j--;
if (i <= j){
swap(vec[i], vec[j]);
i++;
j--;
}
}
if (j > l)
quickSort(vec, l, j);
if (i < r)
quickSort(vec, i, r);
}
void mergeSort(vector<pair<string, string>> &vec, int l, int r) {
if (l == r)
return;
int m = (l + r) / 2;
mergeSort(vec, l, m);
mergeSort(vec, m + 1, r);
vector<pair<string, string>> new_vec;
for (int i = l, j = m + 1; i <= m || j <= r;) {
if (i > m) {
new_vec.push_back({vec[j].first, vec[j].second});
j++;
}
else if (j > r) {
new_vec.push_back({vec[i].first, vec[i].second});
i++;
}
else if (vec[i].first <= vec[j].first) {
new_vec.push_back({vec[i].first, vec[i].second});
i++;
}
else {
new_vec.push_back({vec[j].first, vec[j].second});
j++;
}
}
for (int i = 0; i < new_vec.size(); i++) {
vec[l + i].first = new_vec[i].first;
vec[l + i].second = new_vec[i].second;
}
}
void file_cin(){
freopen("race.in", "r", stdin);
freopen("race.out", "w", stdout);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
file_cin();
int n;
cin >> n;
vector<pair<string, string> > vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i].first >> vec[i].second;
}
mergeSort(vec, 0, n - 1);
string s = vec[0].first;
for(int i = 0; i < n; i++){
if(vec[i].first != s || i == 0){
s = vec[i].first;
cout << "=== " << vec[i].first << " ===" << '\n' << vec[i].second << '\n';
}
else{
cout << vec[i].second << '\n';
}
}
}