-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_simulation_n.cpp
43 lines (38 loc) · 981 Bytes
/
AC_simulation_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.cpp
* Create Date: 2014-11-29 22:56:28
* Descripton: simulation it
*/
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
string convert(string s, int nRows) {
if (nRows == 1) return s;
int step = nRows * 2 - 2, len = s.length();
string ret = "";
// first row
for (int i = 0; i < len; i += step)
ret += s[i];
for (int i = 1; i < nRows - 1; i++) {
for (int j = i; j < len; j += step) {
ret += s[j];
if (j + (step - i * 2) < len)
ret += s[j + (step - i * 2)];
}
}
// last row
for (int i = nRows - 1; i < len; i += step)
ret += s[i];
return ret;
}
};
int main() {
Solution a;
string s;
int n;
while (cin >> s >> n)
cout << a.convert(s, n) << endl;
return 0;
}