-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguessthedatastructure.cpp
210 lines (165 loc) · 4.16 KB
/
guessthedatastructure.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/**
* @file guessthedatastructure.cpp
* @author William Weston
* @brief I Can Guess the Data Structure! Problem from Kattis
* @version 0.1
* @date 2023-06-19
*
* @copyright Copyright (c) 2023
*
* Source: https: //open.kattis.com/problems/guessthedatastructure
*/
#include <cstdlib>
#include <iostream>
#include <queue> // priority_queue, queue
#include <stack> // stack
#include <string>
//#include <type_traits> // same_as
#include <tuple>
#include <utility>
#include <vector>
struct Command final
{
enum operation
{
in,
out
};
operation op;
int param;
};
auto operator>>( std::istream& in, Command& cmd )->std::istream&;
class Solver final
{
public:
explicit Solver( std::vector<Command>&& cmds ) noexcept
: cmds_( std::move( cmds) ) {}
auto solve() -> void;
private:
std::vector<Command> cmds_ = {};
};
struct Results final
{
bool is_pq;
bool is_stack;
bool is_queue;
};
struct Containers final
{
std::priority_queue<int> pq_ = {};
std::stack<int> stack_ = {};
std::queue<int> queue_ = {};
auto add( int value ) -> void;
auto remove( int expected ) -> Results;
};
template <typename... Args> constexpr auto
XOR( Args... args ) -> bool
// requires ( std::same_as<bool, Args> && ... )
{
return ( ( args != ... ) && !( args && ...) );
}
auto
main() -> int
{
auto N = 0; // number of commands for a given test case
while ( std::cin >> N )
{
auto cmds = std::vector<Command>();
for ( auto idx = 0; idx < N; ++idx )
{
auto tmp = Command();
std::cin >> tmp;
cmds.push_back( tmp );
}
auto solver = Solver( std::move( cmds ) );
solver.solve();
}
return EXIT_SUCCESS;
}
auto
operator>>( std::istream& in, Command& cmd ) -> std::istream&
{
int i;
in >> i >> cmd.param;
switch ( i )
{
case 1:
cmd.op = Command::in;
break;
case 2:
cmd.op = Command::out;
break;
defaule:
std::cerr << "Error! Unknown Operation";
break;
}
return in;
}
auto
Solver::solve() -> void
{
using namespace std::literals;
auto containers = Containers();
bool is_pq, is_stack, is_queue = true;
for ( auto cmd : cmds_ )
{
auto [op, param] = cmd;
switch( op )
{
case Command::in:
{
containers.add( param );
}
break;
case Command::out:
{
auto const [pq_result, stack_result, queue_result] = containers.remove( param );
is_pq = ( is_pq ) ? pq_result : false;
is_stack = ( is_stack ) ? stack_result : false;
is_queue = ( is_queue ) ? queue_result : false;
}
break;
default:
{
std::cerr << "Error: Unknown Command";
}
} // switch
} // for
// case impossible: is_pq && is_stack && is_queue == false
if ( !( is_pq || is_stack || is_queue ) )
{
std::cout << "impossible\n";
}
// case only 1 correct answer
else if ( XOR( is_pq, is_stack, is_queue ) ) // ( ( is_pq ^ is_stack ^ is_queue ) ^ ( is_pq && is_stack && is_queue ) ) // WRONG!!! Requires logical XOR
{
std::cout << ( ( is_pq ) ? "priority queue\n"s :
( is_stack ) ? "stack\n"s
: "queue\n"s );
}
// case not sure ( multiple correct answers)
else
{
std::cout << "not sure\n";
}
}
auto
Containers::add( int value ) -> void
{
pq_.push( value );
stack_.push( value );
queue_.push( value );
}
auto
Containers::remove( int expected ) -> Results
{
if ( !pq_.size() ) // access attempt on empty containers
return Results{ false, false, false };
auto const pq_result = pq_.top();
auto const stack_result = stack_.top();
auto const queue_result = queue_.front();
pq_.pop();
stack_.pop();
queue_.pop();
return Results{ pq_result == expected, stack_result == expected, queue_result == expected };
}