# 字符串处理器
纯模拟,但情况比较多,非常容易 空指针异常。
大家要注意,边界问题 以及 负数问题。
整体代码如下:
#include<bits/stdc++.h>
using namespace std;
int main() {
int index = 0;
long long optNum;
string s;
string cmd;
while(cin >> cmd){
//cout << s << endl;
if(cmd == "insert") {
string buff;
cin >> buff;
s.insert(index, buff);
index += buff.size();
}
else if(cmd == "move") {
cin >> optNum;
if(optNum > 0 && index + optNum <= s.size()) index += optNum;
if(optNum < 0 && index >= -optNum) index += optNum;
}
else if(cmd == "delete") {
cin >> optNum;
if(index >= optNum && optNum > 0){
s.erase(index - optNum, optNum);
index -= optNum;
}
}
else if(cmd == "copy") {
if(index > 0) {
string tmp = s.substr(0, index);
s.insert(index, tmp);
}
}
else if(cmd == "end") {
for(int i = 0; i < index; i++) {
cout << s[i];
}
cout << '|';
for(int i = index; i < s.size(); i++) cout << s[i];
break;
}
}
return 0;
}
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
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
# 其他语言版本
# Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
StringBuilder s = new StringBuilder();
int index = 0;
int optNum;
while (true) {
String cmd = scanner.next();
if (cmd.equals("insert")) {
String buff = scanner.next();
s.insert(index, buff);
index += buff.length();
} else if (cmd.equals("move")) {
optNum = scanner.nextInt();
if (optNum > 0 && index + optNum <= s.length()) index += optNum;
if (optNum < 0 && index >= -optNum) index += optNum;
} else if (cmd.equals("delete")) {
optNum = scanner.nextInt();
if (index >= optNum && optNum > 0) {
s.delete(index - optNum, index);
index -= optNum;
}
} else if (cmd.equals("copy")) {
if (index > 0) {
String tmp = s.substring(0, index);
s.insert(index, tmp);
}
} else if (cmd.equals("end")) {
System.out.print(s.substring(0, index) + '|' + s.substring(index));
break;
}
}
scanner.close();
}
}
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
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
# Python
def main():
import sys
input = sys.stdin.read
data = input().split()
s = ""
index = 0
i = 0
while i < len(data):
cmd = data[i]
i += 1
if cmd == "insert":
buff = data[i]
i += 1
s = s[:index] + buff + s[index:]
index += len(buff)
elif cmd == "move":
optNum = int(data[i])
i += 1
if optNum > 0 and index + optNum <= len(s):
index += optNum
elif optNum < 0 and index >= -optNum:
index += optNum
elif cmd == "delete":
optNum = int(data[i])
i += 1
if index >= optNum and optNum > 0:
s = s[:index - optNum] + s[index:]
index -= optNum
elif cmd == "copy":
if index > 0:
tmp = s[:index]
s = s[:index] + tmp + s[index:]
elif cmd == "end":
print(s[:index] + '|' + s[index:])
break
if __name__ == "__main__":
main()
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
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
@2021-2025 代码随想录 版权所有 粤ICP备19156078号