给定长度为n的只有小写字母的字符串s,进行m次操作,每次将[l,r]范围内所有c1字符改成c2,输出操作完的字符串
第一行两个数n,m
第二行一个字符串s
之后m行,每行两个数l r两个字符c1 c2
输出一行一个字符串
5 3 wxhak 3 3 h x 1 5 x a 1 3 w g
gaaak
第一次修改后为wxxak
第二次修改后为waaak
第三次修改后为gaaak
对于100%的数据,有1<=n , m<=100
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(); in.nextLine(); StringBuilder str = new StringBuilder( in.nextLine()); //stringbuilder 可修改的字符串 for (int i = 0; i < m; i++) { int l = in.nextInt(), r = in.nextInt(); char c1 = in.next().charAt(0), c2 = in.next().charAt(0); //替换和修改的字符 for (int j = l - 1; j < r; j++) { if (str.charAt(j) == c1) { str.setCharAt(j, c2); //重置该位置的字符 } } } System.out.println(str); } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(),m=sc.nextInt(); String[] arr=sc.next().split(""); for(int i=0;i<m;i++){ int l=sc.nextInt(),r=sc.nextInt(); String c1=sc.next(),c2=sc.next(); for(int j=l-1;j<=r-1;j++){ if(arr[j].equals(c1)) arr[j]=c2; } } for(String s:arr) System.out.print(s); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); String str = scanner.next(); char[] c = str.toCharArray(); for(int i = 0;i < m;i++){ int l = scanner.nextInt(); int r = scanner.nextInt(); char c1 = scanner.next().charAt(0); char c2 = scanner.next().charAt(0); for(int j = l - 1;j < r ;j++){ if(c[j] == c1){ c[j] = c2; } } } System.out.println(c); } }