【模拟】第二届全国高校绿色计算大赛 预赛第二阶段(Python) 文本编辑器

传送门

这是一个模拟题,因为之前做过一个分块题目,直接想错了。纠结了好久,今天上午突然明悟过来

题意

image-20200825184537784
image-20200825184537784

思路

 看数据大小,直接模拟链表。
 最前和最后添加上一个节点,这样写起来比较好
 相当于复习了一遍 c 语言学的链表,写起来还是挺恶心的。

代码

  1'''
2Autor: valk
3Date: 2020-08-19 18:23:25
4LastEditTime: 2020-08-25 18:21:45
5Description: 如果邪恶  是华丽残酷的乐章 它的终场 我会亲手写上 晨曦的光 风干最后一行忧伤 黑色的墨 染上安详
6'''

7class Task(object):#print
8    def solver(self, s, p, ops):
9        N = 3 * (10**5) + 10
10        pre = [0 for i in range(N)]
11        nex = [0 for i in range(N)]
12        t = [0 for i in range(N)]
13        aga = 0
14        pre[aga] = -1
15        nex[aga] = 1
16        n = len(s)
17        for i in range(len(s)):
18            t[i + 1] = s[i]
19            pre[i + 1] = i
20            nex[i + 1] = i + 2
21        en = n + 1
22        pre[en] = n
23        nex[en] = -1
24        n += 1
25        def insert(text):
26            nonlocal n, pre, nex, aga, en, t, N, p
27            rec = nex[p]
28            nex[p] = n + 1
29            pre[n + 1] = p
30            for i in range(len(text)):
31                n += 1
32                t[n] = text[i]
33                if i != 0:
34                    pre[n] = n - 1
35                nex[n] = n + 1
36            nex[n] = rec
37            pre[rec] = n
38            p = n
39        def back(l):
40            nonlocal n, pre, nex, aga, en, t, N, p
41            i = 1
42            tmp = p
43            while (i <= l and pre[tmp] != -1):
44                tmp = pre[tmp]
45                i += 1
46            nex[tmp] = nex[p]
47            pre[nex[p]] = tmp
48            p = tmp
49        def delete(l):
50            nonlocal n, pre, nex, aga, en, t, N, p
51            i = 0
52            tmp = p
53            while (i <= l and nex[tmp] != -1):
54                tmp = nex[tmp]
55                i += 1
56            if tmp == p: # 特判这里,否则会死循环
57                return
58            nex[p] = tmp
59            pre[tmp] = p
60        def shiftL(l):
61            nonlocal n, pre, nex, aga, en, t, N, p
62            i = 1
63            while (i <= l and pre[p] != -1):
64                p = pre[p]
65                i += 1
66        def shiftR(l):
67            nonlocal n, pre, nex, aga, en, t, N, p
68            i = 1
69            while (i <= l and nex[p] != en):# 这里不能跑到最后一个
70                p = nex[p]
71                i += 1
72        for op in ops:
73            if op[0] == 'I':
74                insert(op[1])
75            if op[0] == 'B':
76                back(int(op[1]))
77            if op[0] == 'D':
78                delete(int(op[1]))
79            if op[0] == 'L':
80                shiftL(int(op[1]))
81            if op[0] == 'R':
82                shiftR(int(op[1]))
83        now = nex[aga]
84
85        ans = ''
86        while (now != en):
87            ans += str(t[now])
88            now=nex[now]
89        return ans
90'''
91s = input()
92pos = int(input())
93t = int(input())
94ops = []
95for i in range(t):
96    op = list(input().split())
97    ops.append(op)
98valk = Task()
99print(valk.solver(s, pos, ops))
100'''

101'''
102whatsyourproblem
1035
1046
105L 2
106D 1
107R 4
108I abcdef
109L 3
110B 2
111'''

原文地址:https://www.cnblogs.com/valk3/p/13561348.html