Java 洛谷P5737 文字处理软件

题目描述:
你需要开发一款文字处理软件。最开始时输入一个字符串(不超过 100 个字符)作为初始文档。可以认为文档开头是第 0 个字符。需要支持以下操作:

1 str:后接插入,在文档后面插入字符串 str,并输出文档的字符串。

2 a b:截取文档部分,只保留文档中从第 a 个字符起 b 个字符,并输出文档的字符串。

3 a str:插入片段,在文档中第 a 个字符前面插入字符串 str,并输出文档的字符串。

4 str:查找子串,查找字符串 str 在文档中最先的位置并输出;如果找不到输出 -1。

为了简化问题,规定初始的文档和每次操作中的 str 都不含有空格或换行。最多会有 q(q\le100)q(q≤100) 次操作。
输入

4
ILove
1 Luogu
2 5 5
3 3 guGugu
4 gu

输出

ILoveLuogu
Luogu
LuoguGugugu
3

代码

package Main;

import java.util.Scanner;

public class Main {
	static Scanner in = new Scanner(System.in);
	static int n = in.nextInt();
	static String str = in.next();
	public static void main(String[] args) {
		for(int i = 0;i < n; i++) {
			switch(in.nextInt()){
			case 1:{
				f1(in.next());
				break;
			}
			case 2:{
				f2(in.nextInt(), in.nextInt());
				break;
			}
			case 3:{
				f3(in.nextInt(), in.next());
				break;
			}
			case 4:{
				f4(in.next());
				break;
			}
			}
		}
	}
	static void f1 (String s) {
		str = str + s;
		System.out.println(str);
	}
	static void f2(int a,int b) {
		str = str.substring(a,a+b);
		System.out.println(str);
	}
	static void f3 (int num,String s) {
		String s1 = str.substring(0,num);
		String s2 = str.substring(num,str.length());
		str = s1 + s + s2;
		System.out.println(str);
	}
	static void f4 (String s) {
		System.out.println(str.indexOf(s));
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值