现在有一个字符串,你要对这个字符串进行 n 次操作,每次操作给出两个数字:(p, l) 表示当前字符串中从下标为 p 的字符开始的长度为 l 的一个子串。你要将这个子串左右翻转后插在这个子串原来位置的正后方,求最后得到的字符串是什么。字符串的下标是从 0 开始的,你可以从样例中得到更多信息。
现在有一个字符串,你要对这个字符串进行 n 次操作,每次操作给出两个数字:(p, l) 表示当前字符串中从下标为 p 的字符开始的长度为 l 的一个子串。你要将这个子串左右翻转后插在这个子串原来位置的正后方,求最后得到的字符串是什么。字符串的下标是从 0 开始的,你可以从样例中得到更多信息。
每组测试用例仅包含一组数据,每组数据第一行为原字符串,长度不超过 10 ,仅包含大小写字符与数字。接下来会有一个数字 n 表示有 n 个操作,再接下来有 n 行,每行两个整数,表示每次操作的(p , l)。
保证输入的操作一定合法,最后得到的字符串长度不超过 1000。
输出一个字符串代表最后得到的字符串。
ab 2 0 2 1 3
abbaabb
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { string st; while (cin >> st) { int n; cin >> n; while (n--) { int beg, len, index; cin >> beg >> len; string temp = st.substr(beg, len); index = beg + len; reverse(temp.begin(), temp.end()); st.insert(index, temp); } cout << st << endl; } return 0; }
var lines=[] while(line=readline()){ lines.push(line) } var str = lines[0] var n = lines[1] for (let i = 2; i < lines.length; i++) { var t = lines[i] var t_arr=t.split(' ') var p=parseInt(t_arr[0]) var l=parseInt(t_arr[1]) var end=p+l var prefix=str.slice(0,end) var suffix=str.substr(end) var temp=prefix.concat(str.slice(p,end).split('').reverse().join('')).concat(suffix) str=temp } console.log(str)
在c编程珠玑中,将字符串中的两个相邻位置子串交换,例如将abc123位置交换可以这样做:首先将 abc反转,变成cba123,再将123反转,变成cba321,最后,将整个子串反转,即变为123abc. 十分方便!(AB--A'B---A'B'---BA) 这个题可以将反转之后的子串接在整个串后面,再将子串进行反转交换就可以得到结果。 #include <stdio.h> #include <string> #include <iostream> using namespace std; string swapstring(string substring,int startp,int l) { int start, i; for(start =startp,i=1;i<=l/2;i++,start++) { char tmp = substring[start]; substring[start] =substring[l+startp-i]; substring[l+startp-i]=tmp; } return substring; } string revinsert(string s,int p,int l) { int len = s.length(); string substring =s.substr(p,l); string resstring =s.substr(l+p,len); substring=swapstring(substring,0,l); s=s+substring; s=swapstring(s,p+l,resstring.length()); s=swapstring(s,len,substring.length()); s=swapstring(s,p+l,substring.length()+resstring.length()); return s; } int main() { string s; int n; cin>>s; cin>>n; while (n>0) { int p,l; cin>>p>>l; s = revinsert(s,p,l); n--; } cout<<s<<endl; return 0; }
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ StringBuffer str =new StringBuffer( in.nextLine()); int n = in.nextInt(); for(int i =0;i<n;i++){ int start = in.nextInt(); int end = start + in.nextInt(); StringBuffer buffer = new StringBuffer(str.substring(start, end)); str.insert(end, buffer.reverse().toString()); } System.out.println(str); } } }
s = input() n = int(input()) for _ in range(n): p, l = map(int, input().strip().split()) s = s[:p + l] + s[p:p + l][::-1] + s[p + l:] print(s)java版:
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 str = br.readLine().trim(); int n = Integer.parseInt(br.readLine().trim()); for(int i = 0; i < n; i++){ String[] temp = br.readLine().trim().split(" "); int p = Integer.parseInt(temp[0]), l = Integer.parseInt(temp[1]); str = str.substring(0, p + l) + reverse(str.substring(p, p + l)) + str.substring(p + l); } System.out.println(str); } private static String reverse(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } }
let input = []; while(line=readline()){ input.push(line); } let s = input.shift(); let n = input.shift(); for(let i = 0; i < n; i++){ let temp = input[i].split(" "); let total = Number(temp[0])+Number(temp[1]); let result = s.substr(temp[0], temp[1]).split('').reverse().join(''); s = s.slice(0, total)+result+s.slice(total); } print(s);
process.stdin.resume(); process.stdin.setEncoding('ascii'); var input = ""; var input_array = ""; process.stdin.on('data', function(data) { input += data; }); process.stdin.on('end', function() { input_array = input.split("\n"); var nLine = 0; while (nLine < input_array.length) { var line = input_array[nLine++].trim(); if (line === '') { continue; } var s = line; var n = +input_array[nLine++]; while (n--) { var input_arrays = input_array[nLine++].trim().split(' '); var p = +input_arrays[0]; var l = +input_arrays[1]; //你的代码 var arr = s.split('') var str = s.substr(p, l).split('').reverse().join('') arr.splice(p + l, 0, str) s = arr.join('') } console.log(s) } });
import java.util.Scanner; public class JRTT1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String s = scanner.nextLine(); int n = scanner.nextInt(); int[][] op = new int[n][2]; for (int i = 0; i < n; i++) { op[i][0] = scanner.nextInt(); op[i][1] = scanner.nextInt(); } System.out.println(decodeString(s, n, op)); } } public static String decodeString(String s, int n, int[][] op) { StringBuffer buffer = new StringBuffer(s); for (int i = 0; i < n; i++) { StringBuffer addStr = new StringBuffer(buffer.substring(op[i][0], op[i][0] + op[i][1])).reverse(); buffer.insert(op[i][0] + op[i][1], addStr); } return buffer.toString(); } }
importjava.util.*;publicclassMain{publicstaticvoidmain(String[] args){Scanner sc=newScanner(System.in);StringBuffer str=newStringBuffer();intn,p,l;while(sc.hasNext()){str.append(sc.next());n=sc.nextInt();while(n-->0){p=sc.nextInt();l=sc.nextInt();str=newStringBuffer(str.substring(0,p+l)+newStringBuffer(str.substring(p, p+l)).reverse()+str.substring(p+l));}System.out.println(str);}}}
#include<iostream>#include<string>using namespace std;intmain(){string str;cin>>str;intn;cin>>n;intp,l;while(n--){cin>>p>>l;string str1=str.substr(p,l),str2=str1;intlen=str1.size();for(inti=0;i<len;++i)str2[i]=str1[len-i-1];str.insert(p+l,str2);}cout<<str<<endl;return0;}
var line = readline();var n = readline();for(var i=0;i<n;i++){var line2 = readline().split(' ');var start = parseInt(line2[0]);var len = parseInt(line2[1]);var temp = line.substr(start,len).split('').reverse().join('');line = line.slice(0,start+len) + temp + line.slice(start+len);}print(line);
var first = readline()var second = readline()function re(str, start, leng) {var s = str.substr(start, leng)s = s.split('').reverse().join('')var tou=str.slice(0,start+leng)var wei=str.slice(start+leng,str.length)first = tou.concat(s,wei);}for(var i=0;i<second;i++){var arr = readline().split(' ');var start = parseInt(arr[0]);var leng = parseInt(arr[1]);re(first, start, leng)}print(first)
语言:Java 运行时间: 25 ms 占用内存:402K 状态:答案正确importjava.util.Scanner;public class Main{public static void main(String[]args){Scanner a = new Scanner(System.in);StringBuffer bf = new StringBuffer(a.next());int n = a.nextInt();a.nextLine();for(int i=0; i<n; i++){String[] aa = a.nextLine().split(" ");int p = Integer.parseInt(aa[0]);int l = Integer.parseInt(aa[1]);StringBuffer sb= newStringBuffer(bf.substring(p,p+l));sb.reverse();bf.insert(p+l,sb);}System.out.println(bf.toString());}}
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void async function () { // Write your code here line = await readline() let arr = line.split(''); let newarr=JSON.parse(JSON.stringify(arr)) let str='' line=await readline() while(line = await readline()){ let [p,l] = line.split(' '); let arrsli=arr.slice(p,l+p) newarr.push(...arrsli.reverse()) arr=newarr } newarr.map(n=>{ str+=n }) console.log(str) }()js v8
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char* s = (char*)malloc(sizeof(char)*1000); int n,m,l; scanf("%s",s); s[strlen(s)] = '\0'; scanf("%d",&n); for(int i = 0; i < n; i++) { scanf("%d%d",&m,&l); char* substr = (char*)malloc(sizeof(char)*(l+1)); int j = 0; int len =l; while(len--) { substr[j] = s[m+l-1]; j++; m--; } s = strcat(s,substr); s[strlen(s)] = '\0'; } printf("%s",s); return 0; }