-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathprogressivescramble.cc
40 lines (35 loc) · 1014 Bytes
/
progressivescramble.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
// https://open.kattis.com/problems/progressivescramble
#include <iostream>
#include <vector>
using namespace std;
typedef vector<int> vi;
string encrypt(const string &s) {
vi a(s.size());
string t(s.size(), ' ');
for (int i = 0; i < s.size(); i++) a[i] = s[i] == ' ' ? 0 : s[i] - 'a' + 1;
for (int i = 1; i < s.size(); i++) a[i] = (a[i] + a[i - 1]) % 27;
for (int i = 0; i < s.size(); i++) t[i] = a[i] ? 'a' + a[i] - 1 : ' ';
return t;
}
string decrypt (const string &s) {
vi a(s.size());
string t(s.size(), ' ');
for (int i = 0; i < s.size(); i++) a[i] = s[i] == ' ' ? 0 : s[i] - 'a' + 1;
for (int i = s.size() - 1; i >= 0; i--) {
a[i] -= a[i - 1];
if (a[i] < 0) a[i] += 27;
}
for (int i = 0; i < s.size(); i++) t[i] = a[i] ? 'a' + a[i] - 1 : ' ';
return t;
}
int main() {
int n;
string s;
cin >> n;
getline(cin, s);
while (n--) {
getline(cin, s);
if (s[0] == 'e') cout << encrypt(s.substr(2, s.size() - 2)) << endl;
else cout << decrypt(s.substr(2, s.size() - 2)) << endl;
}
}