给定长度为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
#include <stdio.h> int main(){ int n, count, l, r; char a, b, arr[100];; scanf("%d %d", &n, &count); scanf("%s", arr); while(count--){ scanf("%d %d %c %c", &l, &r, &a, &b); for(int i = l-1; i <= r-1; i++){ if(arr[i] == a) arr[i] = b; } } printf("%s", arr); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m, l, r; string s; cin >> n >> m >> s; char c1, c2; for (int i = 0; i < m; ++i) { cin >> l >> r >> c1 >> c2; for (int j = l - 1; j <= r - 1; ++j) { if (s[j] == c1) s[j] = c2; } } cout << s << endl; return 0; }
#include<stdio.h> int main() { int n, m, l, r; scanf("%d %d", &n, &m); char str[100] = {0}, c1, c2; scanf("%s", str); while (m--) { scanf("%d %d %c %c", &l, &r, &c1, &c2); for (int j = l - 1; j <= r - 1; j++) if (str[j] == c1) str[j] = c2; } printf("%s\n", str); return 0; }
#include <stdio.h> int main() { int chang, ci,zi1,zi2;char zifu[200]={},th1,th2; //获取长度和字符串 scanf("%d%d",&chang,&ci); getchar(); scanf("%s",zifu); //获取字符范围与需交换字符值 for (int y=0; y<ci; y++) { scanf("%d%d %c %c",&zi1,&zi2,&th1,&th2); for (int x=zi1; x<=zi2; x++) { if (zifu[x-1]==th1) { zifu[x-1]=th2; } } } //输出操作玩的字符串 printf("%s",zifu); return 0; }
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); } }
#include <stdio.h> #include <string.h> int main() { int n = 0; int m = 0; scanf("%d %d", &n, &m); char str[100]; // for(int i = 0; i<n; ++i) // { // scanf(" %c", &str[i]); // } getchar(); gets(str); for(int i = 0; i<m; ++i) { int p, q; char c1, c2; scanf("%d %d %c %c", &p, &q, &c1, &c2); for(int j = p; j<=q; ++j) { if(str[j-1] == c1) { str[j-1] = c2; } } } // for(int i = 0; i<n; ++i) // { // printf("%c", str[i]); // } puts(str); return 0; }这边注意那个输入有问题,前面有个回车键
#include <stdio.h> #include <stdlib.h> int main() { int n , m, r, l; char c1, c2; int i = 0; char* p = NULL; scanf("%d%d", &n, &m); p = (char*)malloc(sizeof(char) * (n + 1)); if(!p) { perror("malloc fail!"); return 1; } scanf("%s", p); //进行m次后续操作 for(i = 0; i < m; i++) { scanf("%d%d %c %c", &r, &l, &c1, &c2); //查找r到l中的c1 while(r <= l) { //找到了,进行交换 if(*(p + r - 1) == c1) { *(p + r - 1) = c2; } r++; } } printf("%s\n", p); free(p); p = NULL; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n = 0, m = 0, l = 0, r = 0; cin >> n >> m; char* s = (char*)malloc(n * sizeof(char)); char c1, c2; cin>>s; for (int i = 0; i < m; i++) { cin >> l >> r >> c1 >> c2; for (int j = l - 1; j < r; j++) { if (s[j] == c1) { s[j] = c2; } } } cout<<s<<endl; return 0; }
#include <stdio.h> int main() { int n, m; scanf("%d %d", &n, &m); char arr[n+1]; for(int i=1; i<=n; i++){ scanf(" %c", &arr[i]); } for(int i=0; i<m; i++){ int p, q; char a, b; scanf("%d %d %c %c", &p, &q, &a, &b); for(int j=p; j<=q; j++){ if(arr[j] == a){ arr[j] = b; } } } for(int i=1; i<=n; i++){ printf("%c", arr[i]); } return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> int main() { int n = 0;//字符串的长度 int m = 0;//进行m 次 操作 scanf("%d %d", &n, &m); getchar();//吸收换行符 char s[101] = { 0 }; //向数组中输入字符串 for (int i = 1; i <= n; i++) { scanf("%c", &s[i]); } for (int i = 1; i <= m; i++) { int l = 0; int r = 0;//左右范围 char c1 = 0; char c2 = 0;//将 c1 字符 改为 c2 字符 scanf("%d %d\n%c\n%c", &l, &r, &c1, &c2);//注意吸收换行符 //遍历 左右 区间内的 字符 并进行更改 for (int j = l; j <= r; j++) { if (s[j] == c1) { s[j] = c2; } } } //输出 for (int i = 1; i <= n; i++) { printf("%c", s[i]); } return 0; }
#include <stdio.h> int main() { //n决定字符串长度,m决定替换次数 int n, m; //字符串 char s[100]; //判断范围 int l,r; //当l到r的范围内有满足c1的则替换成c2 char c1, c2; //输入字符串长度,替换次数 scanf("%d %d",&n,&m); getchar(); //获取字符串内容 for(int i = 0;i<n;i++) { scanf("%c",&s[i]); } //替换次数 for(int j=0;j<m;j++) { //获取范围 scanf("%d %d",&l,&r); //获取c1 getchar(); scanf("%c",&c1); //获取c2 getchar(); scanf("%c",&c2); getchar(); //对l到r范围内等于c1的替换成c2 for(int i = l-1;i<r;i++) { if(s[i]==c1) { s[i]=c2; } } } //打印 for(int i=0;i<n;i++) { printf("%c",s[i]); } return 0; }
package main import ( "fmt" ) func main() { var n,m int fmt.Scan(&n,&m) bytes := make([]byte, n) for i:=0;i<n;i++{ fmt.Scanf("%c", &bytes[i]) } // 读掉字符串的 \n fmt.Scanf("\n") for i:=0;i<m;i++{ var l,r int var c1,c2 byte fmt.Scanf("%d %d %c %c\n",&l,&r,&c1,&c2) for i:=l-1;i<=r-1;i++{ if bytes[i] == c1 { bytes[i] = c2 } } } for i:=0;i<n;i++{ fmt.Printf("%c", bytes[i]) } }
#include <stdio.h> int main() { int m,n; char arr[200]={0}; scanf("%d %d",&n,&m); scanf("%s",arr); for(int j=0;j<m;j++) { int l,r; char c1,c2; scanf("%d %d %c %c",&l,&r,&c1,&c2); for(int i=l-1;i<r;i++) { if(arr[i]==c1) { arr[i]=c2; } } } printf("%s",arr); return 0; }
#include <stdio.h> int main() { int n,m; scanf("%d %d",&n,&m); getchar();//注意要把第一个输入的回车吃掉,不然后面没法运行 char arr[100]; gets(arr); int i,l,r; char c1,c2; for(i=0;i<m;i++)//趟数 { scanf("%d %d %c %c",&l,&r,&c1,&c2); int j; for(j=l-1;j<r;j++)//识别并进行赋值 { if(arr[j]==c1) { arr[j] = c2; } } } for(i=0;i<n;i++)//循环打印 { printf("%c",arr[i]); } return 0; }
#include <stdio.h> int main() { int n = 0; int m = 0; scanf("%d %d", &n, &m); getchar(); char arr[100] = { 0 }; gets(arr); int l = 0; int r = 0; char c1 = 0; char c2 = 0; int i = 0; for (i = 0; i < m; i++) { if (scanf("%d %d %c %c", &l, &r, &c1, &c2) == 4) { int x = 0; for (x = l - 1; x <= r - 1; x++) { if (arr[x] == c1) { arr[x] = c2; } } } } printf("%s\n", arr); return 0; }
def mod_str(str_in, l, r, a, b): for i in str_in[l - 1 : r]: if i == a: str_in[str_in[l - 1 : r].index(i) + l - 1] = b return str_in if __name__ == "__main__": n, m = list(map(int, input().split())) in_str = input() out_str = list(in_str) for i in range(m): l, r, a, b = input().split() l = int(l) r = int(r) out_str = mod_str(out_str, l, r, a, b) print("".join(out_str))