前言

今天再做以一道入门题,主要是好久没做算法题目了,核心算法都忘记的差不多了,慢慢增加难度去攻破难题。

题目链接

分析

我们先来看下输入输出样例

1
2
3
4
5
6
7
输入:
4
ILove
1 Luogu
2 5 5
3 3 guGugu
4 gu
1
2
3
4
5
输出:
ILoveLuogu
Luogu
LuoguGugugu
3

这道题题目并没有给太多的描述,不难看出,4代表一共四条指令,第二行字符代表初始字符,依次是几次操作。

得以于Java原生支持的字符操作,这道题确实很简单,直接看代码。

代码

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
import java.util.Scanner;

/**
* Created By XuanRan on 2021/12/10
*/
public class P5734文字处理软件 {
public static StringBuilder sb = new StringBuilder();
public static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {
int N = sc.nextInt();
sb.append(sc.next());
for (int i = 1; i <= N; i++) {
int index = sc.nextInt();
switch (index) {
case 1:
AppendStr();
break;
case 2:
SubString();
break;
case 3:
InsertStr();
break;
case 4:
FindStr();
break;
}
}
}

private static void FindStr() {
String str = sc.next();
if (!sb.toString().contains(str)){
System.out.println(-1);
return;
}
int index = sb.toString().indexOf(str);
System.out.println(index);
}

private static void InsertStr() {
int x = sc.nextInt();
String y = sc.next();
sb.insert(x,y);
System.out.println(sb.toString());
}

private static void SubString() {
int begin = sc.nextInt();
int end = sc.nextInt();
sb = new StringBuilder(sb.substring(begin, begin + end));
System.out.println(sb);
}

private static void AppendStr() {
String x = sc.next();
sb.append(x);
System.out.println(sb.toString());
}
}

代码很简单,并没有什么难理解的地方,这里就不多解释了。

结语

明天见。