小明同学需要对一个长度为 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
#include <stdio.h> int main() { int length; int op; scanf("%d %d", &length, &op); char str_in[10000] = { 0 }; char str_temp[10000] = { 0 }; char print_str[10000] = { 0 }; gets(str_in); int i = 0; int j = 0; int go_op[1100][2] = { 0 }; for (i = 0; i < length; i++) { scanf("%c", &str_in[i]); } for (i = 0; i < length; i++) { str_temp[i] = str_in[i]; } int result_count = 0; for (i = 0; i < op; i++) { scanf("%d %d", &go_op[i][0], &go_op[i][1]); if (go_op[i][0] == 1) { for (j = 0; j < length - go_op[i][1]; j++) { str_in[length + j] = str_in[j]; } for (j = 0; j < length; j++) { str_temp[j] = str_in[j + length - go_op[i][1]]; } for (j = 0; j < length; j++) { str_in[j] = str_temp[j]; } } else if (go_op[i][0] == 2) { // printf("%c", str_temp[go_op[i][1]]); print_str[result_count] = str_temp[go_op[i][1]]; result_count++; } } // printf("%c\n", str_in[87]); for (i = 0; i < result_count; i++) { printf("%c ", print_str[i]); } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().trim().split(" "); int N = Integer.parseInt(params[0]); int T = Integer.parseInt(params[1]); String str = br.readLine().trim(); int n = str.length(); while(T-- > 0){ String[] typeX = br.readLine().trim().split(" "); int type = Integer.parseInt(typeX[0]); int X = Integer.parseInt(typeX[1]); if(type == 1) str = str.substring(n - X) + str.substring(0, n - X); else System.out.println(str.charAt(X)); } } }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] params = br.readLine().trim().split(" "); int N = Integer.parseInt(params[0]); int T = Integer.parseInt(params[1]); String str = br.readLine().trim(); int n = str.length(); // 将字符串视作一个环形队列 int front = 0; while(T-- > 0){ String[] typeX = br.readLine().trim().split(" "); int type = Integer.parseInt(typeX[0]); int X = Integer.parseInt(typeX[1]); if(type == 1){ front += (n - X) % n; }else{ System.out.println(str.charAt((front + X) % n)); } } } }
#include <iostream> #include <string> using namespace std; int main() { int n,T; cin >> n >> T; string str; cin >> str; while(T--) { int x,type; cin >> type >> x; if(type == 2) cout << str[x] << endl; if(type == 1) { string str1 = str.substr(0,n-x); string str2 = str.substr(n-x); str = str2+str1; } } return 0; }
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)); } } } }
package main import ( "fmt" "os" "bufio" ) var in=bufio.NewReader(os.Stdin) func main() { var n,t int fmt.Fscan(in,&n,&t) var s string fmt.Fscan(in,&s) var a,b int for t>0{ fmt.Fscan(in,&a,&b) if a==1{ s=s[len(s)-b:]+s[:len(s)-b] }else{ fmt.Println(string(s[b])) } t-- } }
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 sys in_num = sys.stdin.readline().strip().split() N,T = int(in_num[0]),int(in_num[1]) ori_str = sys.stdin.readline().strip() res = ori_str for i in range(T): pro = sys.stdin.readline().strip().split() sub_pro = int(pro[0]) move_bin = int(pro[1]) if sub_pro == 1: res = res[N-move_bin:] + res[0:N-move_bin] elif sub_pro == 2: print(res[move_bin])
#include<iostream> (720)#include<string> using namespace std; int main(void){ int m, n; cin>>m>>n; string str; cin>>str; int op, X; string temp1, temp2; for (int i = 0; i < n; i++){ cin>>op>>X; if (op == 1){ temp1 = str.substr(0, m-X); temp2 = str.substr(m-X); str = temp2 + temp1; } else{ cout<<str[X]<<endl; } } return 0; }
n,t = list(map(int,input().split())) s = input() for i in range(t): types,x = list(map(int,input().split())) if types==1: s = s[-x:]+s[:-x] else: print(s[x])
#include<bits/stdc++.h> using namespace std; int main(void) { int z, x; cin >> z >> x; string s; cin >> s; vector<int> v1, v2; char c; while (x--) { int c, v; cin >> c >> v; v1.push_back(c); v2.push_back(v); } for (int i = 0; i < v1.size(); i++) { if (v1[i] == 1) { string s1, s2; for (int j = 0; j < z - v2[i]; j++)s1 += s[j]; for (int j = z - v2[i]; j < z; j++)s2 += s[j]; s = ""; s += s2; s += s1; } else cout << s[v2[i]] << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0),cout.tie(0); int N,T; //字符串长度N,操作次数T cin >> N >> T; cin.ignore(); //cin后用getline前一定要ignore吃回车 string str; getline(cin,str); while(T--) { int type,x; cin >> type >> x; if(type == 1) //type=1将字符串的倒数第x字符前的字符全移动到字符串末尾 { string t = str.substr(0,str.length()-x); //cout << t << endl; str = str.substr(str.length()-x,x) + t; //cout << str << endl; } else //type=2将字符串的第x个字符进行输出 { cout << str[x] << endl; } } return 0; }
def f(n,list): for i in list: if i[0]=="1": a= i[0] b=int(i[1]) n=n[-b:]+n[:-b] if i[0]=="2": c=int(i[1]) print( n[c]) if __name__=="__main__": m=list(map(int,input().strip().split(" "))) n=input() list=[] for _ in range(m[1]): list.append(input().strip().split(" ")) f(n,list)