forked from adnanaziz/EPIJudge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_lists_overlap.cc
93 lines (84 loc) · 2.33 KB
/
do_lists_overlap.cc
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
#include <set>
#include <stdexcept>
#include "list_node.h"
#include "test_framework/generic_test.h"
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"
shared_ptr<ListNode<int>> OverlappingLists(shared_ptr<ListNode<int>> l0,
shared_ptr<ListNode<int>> l1) {
// TODO - you fill in here.
return nullptr;
}
void OverlappingListsWrapper(TimedExecutor& executor,
shared_ptr<ListNode<int>> l0,
shared_ptr<ListNode<int>> l1,
shared_ptr<ListNode<int>> common, int cycle0,
int cycle1) {
if (common) {
if (!l0) {
l0 = common;
} else {
auto it = l0;
while (it->next) {
it = it->next;
}
it->next = common;
}
if (!l1) {
l1 = common;
} else {
auto it = l1;
while (it->next) {
it = it->next;
}
it->next = common;
}
}
if (cycle0 != -1 && l0) {
auto last = l0;
while (last->next) {
last = last->next;
}
auto it = l0;
while (cycle0-- > 0) {
if (!it) {
throw std::runtime_error("Invalid input data");
}
it = it->next;
}
last->next = it;
}
if (cycle1 != -1 && l1) {
auto last = l1;
while (last->next) {
last = last->next;
}
auto it = l1;
while (cycle1-- > 0) {
if (!it) {
throw std::runtime_error("Invalid input data");
}
it = it->next;
}
last->next = it;
}
std::set<shared_ptr<ListNode<int>>> common_nodes;
auto it = common;
while (it && common_nodes.count(it) == 0) {
common_nodes.insert(it);
it = it->next;
}
auto result = executor.Run([&] { return OverlappingLists(l0, l1); });
if (!((common_nodes.empty() && result == nullptr) ||
common_nodes.count(result) > 0)) {
throw TestFailure("Invalid result");
}
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"executor", "l0", "l1",
"common", "cycle0", "cycle1"};
return GenericTestMain(args, "do_lists_overlap.cc", "do_lists_overlap.tsv",
&OverlappingListsWrapper, DefaultComparator{},
param_names);
}