forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressivescramble.cpp
58 lines (50 loc) · 1.2 KB
/
progressivescramble.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
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
int n;
char c;
cin >> n;
for(int i = 0; i < n; i++) {
cin >> c;
cin.ignore();
getline(cin, s);
// Convert to int
vector<int> str;
for(auto i : s) {
str.push_back(i - 'a' + 1);
if(i == ' ') {
str[str.size()-1] = 0;
}
}
// Encrypt
if(c == 'e') {
for(int i = 1; i < str.size(); i++) {
str[i] += str[i-1];
}
for(int i = 0; i < str.size(); i++) {
str[i] %= 27;
}
}
// Decrypt
if(c == 'd') {
for(int i = 1; i < str.size(); i++) {
while(str[i] < str[i-1]) {
str[i] += 27;
}
}
for(int i = str.size(); i > 0; i--) {
str[i] -= str[i-1];
}
}
// Convert to char
for(int i = 0; i < str.size(); i++) {
s[i] = str[i] + 'a' - 1;
if(str[i] == 0) {
s[i] = ' ';
}
}
// Print answer
cout << s << endl;
}
}