-
Notifications
You must be signed in to change notification settings - Fork 13
/
saving-snoopy.cpp
57 lines (50 loc) · 1.75 KB
/
saving-snoopy.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
#include <bits/stdc++.h>
int main()
{
int n, mlen;
std::string swap, msg, dcd;
std::map<char, char> repl;
std::cin >> n; std::cin.ignore();
for ( auto i{ 0 }; i < n ; ++i )
getline(std::cin, swap), repl[swap.front()] = swap.back();
std::cin >> mlen; std::cin.ignore();
getline(std::cin, msg);
std::deque<char> queue;
for ( auto it{ std::cbegin(msg) }; std::cend(msg) != it; ++it) {
switch(*it) {
case '+' : break;
case '*' : {
auto lc{ queue.back() };
if ( std::end(repl) != repl.find(lc) ) lc = repl[lc];
dcd += lc;
queue.pop_back();
} break;
case '#' : {
for ( auto i{ 0 }; i < (int)(*(it + 1) - '0'); ++i )
queue.pop_back();
++it;
} break;
case '%' : {
std::deque<char> shuffled;
for ( auto i{ 0 }; i < std::size(queue); i += 2 )
shuffled.push_back(queue[i]);
for ( auto i{ 1 }; i < std::size(queue); i += 2 )
shuffled.push_back(queue[i]);
queue = shuffled;
} break;
default : queue.push_back(*it); break;
}
}
std::istringstream iss(dcd);
std::string curLine, curWord;
while (iss >> curWord) {
if ( std::size(curLine) + std::size(curWord) < 75 ) {
curLine += std::empty(curLine) ? curWord : std::string(' ' + curWord);
} else {
std::cout << curLine << '\n';
curLine = curWord;
}
}
if ( !std::empty(curLine) )
std::cout << curLine << '\n';
}