小明同学需要对一个长度为 N 的字符串进行处理,他需要按照要求执行若干步骤,每个步骤都均为下面 2 种操作中的一种,2 种操作如下:
TYPE 1. 从字符串结尾开始算起,将第 X 个字符之前的字符移动到字符串末尾
TYPE 2. 输出字符串索引为 X 的字符
小明尝试了很久没能完成,你可以帮他解决这个问题吗?
第一行,包含两个整数,字符串的长度 N 和操作次数T;
第二行为要操作的原始字符串;
之后每行都是要执行的操作类型 TYPE 和操作中 X 的值,均为整数。
输入范围:
字符串长度 N:1 <= N <= 10000
操作次数 T:1 <= T <= 10000
操作类型 TYPE:1 <= TYPE<= 2
变量 X:0 <= X < N
操作的执行结果
6 2 xiaomi 1 2 2 0
m
public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int t=sc.nextInt(); String str=sc.next(); for(int i=0;i<t;i++){ int type=sc.nextInt(); int x=sc.nextInt(); if(type==1){ str=str.substring(n-x)+str.substring(0,n-x); }else{ System.out.println(str.charAt(x)); } } sc.close(); }
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int n = Integer.parseInt(s[0]); int t = Integer.parseInt(s[1]); String str = br.readLine(); for (int i = 0; i < t; i++) { String[] s1 = br.readLine().split(" "); int type = Integer.parseInt(s1[0]); int x = Integer.parseInt(s1[1]); if (type == 1) { str = str.substring(n - x) + str.substring(0, n - x); } else { System.out.println(str.charAt(x)); } } } }