-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
61 lines (48 loc) · 1.56 KB
/
solution.cpp
File metadata and controls
61 lines (48 loc) · 1.56 KB
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
# include <iostream>
# include <cmath>
# include <string>
# include <vector>
using namespace std;
////////////////////////////////////////////////////////////////////////////////
pair<int, int> chooseRowsAndCols(int sSize) {
int rows = sqrt(sSize);
if (rows * rows == sSize) {
return {rows, rows};
}
int cols = rows + 1;
if (rows * cols < sSize) {
rows++;
}
return {rows, cols};
}
string encryption(string s) {
// auto [rows, cols] = chooseRowsAndCols(s.size());
pair<int, int> rc = chooseRowsAndCols(s.size());
int rows = rc.first;
int cols = rc.second;
vector<string> resArr;
for (int r = 0; r < rows; r++) {
resArr.push_back(string(s.begin()+cols*r, s.begin()+cols*(r+1)));
}
string res;
for(int c = 0; c < cols; c++) {
for (int r = 0; r*cols+c < s.size(); r++) {
res.push_back(resArr[r][c]);
}
res.push_back(' ');
}
return res;
}
////////////////////////////////////////////////////////////////////////////////
int main() {
// Test case 1
cout << encryption("haveaniceday") << endl; // hae and via ecy
// Test case 2
cout << encryption("feedthedog") << endl; // fto ehg ee dd
// Test case 3
cout << encryption("chillout") << endl; // clu hlt io
// Test case 4
cout << encryption("ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots") << endl; // imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau
return 0;
}
////////////////////////////////////////////////////////////////////////////////