-
Notifications
You must be signed in to change notification settings - Fork 13
/
character-replacement-problem.cpp
87 lines (71 loc) · 2.4 KB
/
character-replacement-problem.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
#include <bits/stdc++.h>
template < typename T >
class Graph {
private:
struct Node {
Node(T val) noexcept : _val{ val } {}
const T _val;
Node* _next{ nullptr };
};
public:
typedef enum { OK, IGNORED, ERROR } STATUS;
Graph() noexcept = default;
~Graph() noexcept {
std::for_each( std::begin(_repl), std::end(_repl), [](auto& it){
if ( nullptr != it.second ) {
delete(it.second);
it.second = nullptr;
}
});
_repl.clear();
}
[[maybe_unused]] STATUS insert(const T& from, const T& to) noexcept {
if ( from == to ) return IGNORED;
if ( auto it{ _repl.find(to) }; std::end(_repl) == it ) { _repl[to] = new Node(to); }
if ( auto it{ _repl.find(from)}; std::end(_repl) == it ) { _repl[from] = new Node(from); }
Node* nFrom{ _repl[from] }, *nTo{ _repl[to] };
if ( nullptr != nFrom->_next ) return ERROR;
nFrom->_next = nTo;
return _integrityCheck( nFrom );
}
T get(const T& from) noexcept {
if ( std::end(_repl) == _repl.find(from) )
return from;
auto ret{ _repl[from] };
while ( nullptr != ret->_next )
ret = ret->_next;
return ret->_val;
}
private:
STATUS _integrityCheck(Node* n) noexcept {
Node* start{ n }, *cur{ n };
if ( nullptr == start ) return ERROR;
while ( nullptr != cur->_next )
if ( cur = cur->_next; cur == start )
return ERROR;
return OK;
}
private:
std::map<T, Node*> _repl;
};
int main()
{
std::string replacements, cur;
getline(std::cin, replacements);
std::vector<std::string> multiline{ ++std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>() };
Graph<char> graph;
std::stringstream ss(replacements);
while (getline(ss, cur, ' ')) {
if ( (2 != std::size(cur)) || (Graph<char>::STATUS::ERROR == graph.insert(cur[0], cur[1]))) {
std::cout << "ERROR\n";
return EXIT_FAILURE;
}
}
std::for_each( std::begin(multiline), std::end(multiline), [&graph](auto& line){
std::for_each( std::begin(line), std::end(line), [&graph](auto& c){
c = graph.get(c);
});
std::cout << line << '\n';
});
}