-
Notifications
You must be signed in to change notification settings - Fork 0
/
54-DisorderedFirstContact.js
66 lines (53 loc) · 1.47 KB
/
54-DisorderedFirstContact.js
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
62
63
64
65
66
class Result {
constructor() {
this.lastLength = 0;
this.lastSubLength = 0;
this.message = "";
}
}
const N = parseInt(readline());
const absN = Math.abs(N);
let MESSAGE = readline();
for (let i = 0; i < absN; i++) {
MESSAGE = N < 0 ? encode(MESSAGE).message : decode(MESSAGE)
}
console.log(MESSAGE)
function encode(message) {
const result = new Result()
let length = 1
const builder = []
for (let i = 0; i < message.length; i += length++) {
const sub = message.substring(i, Math.min(message.length, i + length))
result.lastLength = length
result.lastSubLength = sub.length
if (length % 2 === 0) builder.unshift(sub)
else builder.push(sub)
}
result.message = builder.join("")
return result
}
function decode(message) {
const r = encode(message)
let result
const fromLeft = r.lastLength % 2 === 0
let leftIndex = 0
let rightIndex = message.length
if (fromLeft) {
result = message.substring(leftIndex, r.lastSubLength)
leftIndex += r.lastSubLength;
} else {
result = message.substring(rightIndex - r.lastSubLength)
rightIndex -= r.lastSubLength
}
let times = r.lastLength - 1
while (times > 0) {
if (times % 2 === 0) {
result = message.substring(leftIndex, leftIndex + times) + result
leftIndex += times--
} else {
result = message.substring(rightIndex - times, rightIndex) + result
rightIndex -= times--
}
}
return result
}