首页 > 试题广场 >

字符串操作

[编程题]字符串操作
  • 热度指数:8594 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}给定长度为 n 的只含小写字母的字符串 s,以及正整数 m 次操作。
\hspace{15pt}每次操作给定两个整数 \ell,r 和两个小写字母 c_1,c_2;将字符串 s 在区间 [\ell,r] 内的所有字符 c_1 替换为 c_2
\hspace{15pt}按顺序执行完所有操作后,输出最终的字符串。

输入描述:
\hspace{15pt}在一行输入两个整数 n,m \left(1 \leqq n,m \leqq 100\right)
\hspace{15pt}接下来一行输入一个只含小写字母的字符串 s,长度为 n
\hspace{15pt}再接下来 m 行,每行输入两个整数 \ell,r 和两个字符 c_1,c_2,用空格分隔,其中 1 \leqq \ell \leqq r \leqq nc_1,c_2 为小写字母。


输出描述:
\hspace{15pt}输出一个只含小写字母的字符串,表示执行完所有操作后的最终字符串。
示例1

输入

5 3
wxhak
3 3 h x
1 5 x a
1 3 w g

输出

gaaak

说明

\hspace{8pt}\bullet\,初始字符串为 `wxhak`; 
\hspace{8pt}\bullet\,第 1 次操作将位置 3 上的 `h` 替换为 `x`,得到 `wxxak`;
\hspace{8pt}\bullet\,第 2 次操作将位置 1 至 5 的 `x` 替换为 `a`,得到 `waaak`;
\hspace{8pt}\bullet\,第 3 次操作将位置 1 至 3 的 `w` 替换为 `g`,得到 `gaaak`。

备注:

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);
    }
}

发表于 2024-09-19 00:47:29 回复(0)
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);
    }
}

发表于 2022-08-12 23:27:35 回复(0)
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);
    }
}

发表于 2022-07-05 11:41:27 回复(0)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            int m = sc.nextInt();
            String str = sc.next();
            for(int i = 0; i < m; i++){
                str = solution(str,sc.nextInt()-1,sc.nextInt()-1,sc.next(),sc.next());
            }
            System.out.println(str);
        }
    }
    
    public static String solution(String str,int l, int r, String c1, String c2){
        StringBuilder sb = new StringBuilder();
        int j = 0;
        while(j< str.length() && j < l){
            sb.append(str.charAt(j++));
        }
        while(j< str.length() && j <= r){
            if(str.charAt(j) == c1.charAt(0)){
                sb.append(c2);
                j++;
            } else{
                sb.append(str.charAt(j));
                j++;
            }
        }
        while(j < str.length()){
                sb.append(str.charAt(j++));
            }
        return sb.toString();
    }
}
发表于 2022-03-23 20:43:21 回复(0)