-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1165_Block Reversing (25).cpp
55 lines (47 loc) · 1.22 KB
/
1165_Block Reversing (25).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
#include <cstdio>
#include <string>
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
struct Node {
string address;
string data;
string next;
};
unordered_map<string, Node> L;
int main() {
ios::sync_with_stdio(false);
string root;
int n, k;
cin >> root >> n >> k;
for (int i = 0; i < n; i++) {
Node node;
cin >> node.address >> node.data >> node.next;
L[node.address] = node;
}
vector<pair<string, string>> blocks; // record every blocks, then reverse based on this
int count = 0;
string p = root, begin = root, last;
while (p != "-1") {
count++;
if (count == k) {
blocks.push_back(make_pair(begin, p));
begin = L[p].next;
count = 0;
}
last = p;
p = L[p].next;
}
if (begin != "-1") blocks.push_back(make_pair(begin, last));
for (int i = blocks.size() - 1; i > 0; i--) {
L[blocks[i].second].next = blocks[i-1].first;
}
L[blocks[0].second].next = "-1";
p = blocks[blocks.size() - 1].first;
while (p != "-1"){
cout << p << " " << L[p].data << " " << L[p].next << endl;
p = L[p].next;
}
return 0;
}