-
Notifications
You must be signed in to change notification settings - Fork 0
/
rope.js
57 lines (51 loc) · 1.48 KB
/
rope.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
// Max time used: 33.88/120.00, max memory used: 86659072/536870912.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
terminal: false
});
rl.once('line', (line) => {
const string = line.toString().trim();
rl.once('line', (line) => {
const n = parseInt(line, 10);
if (n === 0) {
console.log(string);
process.exit();
}
const data = [];
rl.on('line', (line) => {
const item = line.toString().trim().split(' ').map(Number);
if (data.length < n) {
data.push(item);
}
if (data.length === n) {
const rope = new Rope(string);
for (let item of data) {
rope.process(item);
}
rope.result();
}
});
});
});
class Rope {
constructor(string) {
this.string = string;
}
result() {
return process.stdout.write(this.string, () => {
process.exit();
});
}
process(item) {
const [i, j, k] = item;
let substring = this.string.substring(i, j + 1);
this.string = this.string.substring(0, i) + this.string.substring(j + 1);
if (k === 0) {
this.string = substring + this.string;
} else {
this.string = this.string.substring(0, k) + substring + this.string.substring(k);
}
}
}
module.exports = Rope;