在一行输入两个整数
![]()
。
接下来一行输入一个只含小写字母的字符串
,长度为
。
再接下来
行,每行输入两个整数
和两个字符
,用空格分隔,其中
,
为小写字母。
输出一个只含小写字母的字符串,表示执行完所有操作后的最终字符串。
5 3 wxhak 3 3 h x 1 5 x a 1 3 w g
gaaak
初始字符串为 `wxhak`;
第 1 次操作将位置 3 上的 `h` 替换为 `x`,得到 `wxxak`;
第 2 次操作将位置 1 至 5 的 `x` 替换为 `a`,得到 `waaak`;
第 3 次操作将位置 1 至 3 的 `w` 替换为 `g`,得到 `gaaak`。
#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 <iostream>
using namespace std;
#include <string>
#include <algorithm>
int main() {
int n,m;
string s;
cin >> n >> m >> s;
for(int i = 0; i < m; ++i){
int a,b;
char c,d;
cin >> a >> b >> c >> d;
replace(s.begin()+a-1, s.begin()+b, c ,d); //注意此处为左开右闭
}
cout << s << endl;
}
#include <string.h>
int main()
{
int n=0,M=0;
scanf("%d %d",&n,&M);
char s[n+1];
scanf(" %s",s);
for(int m=0;m<M;m++)
{
int l=0,r=0;
char c1=0,c2=0;
scanf("%d %d %c %c",&l,&r,&c1,&c2);
for(int i=l-1;i<=r-1;i++)
{
if(s[i]==c1)
{
s[i]=c2;
}
}
}
printf("%s",s);
return 0;
} 注意一下,在循环里l,r要-1,这才能对应上位置。(*/ω\*)
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int length = in.nextInt();
int round = in.nextInt();
String str = in.next();
for (int i = 1; i <= round; i++) {
int start = in.nextInt() - 1;
int end = in.nextInt() - 1;
String oldLetter = in.next();
String newLetter = in.next();
str = str.substring(0, start) + str.substring(start, end + 1).replace(oldLetter,
newLetter) + str.substring(end + 1);
}
System.out.print(str);
}
} #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 <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 <iostream>
using namespace std;
int main() {
int n , m , l , r ;
char f , t ;
cin >> n >> m;
string aim ;
cin >> aim ;
for (int i = 0 ;i<m ; i++) {
cin >> l >> r >> f >> t ;
for (auto it = aim.begin()+l-1 ;it != aim.begin()+r ; it++) {
if (*it == f) {*it=t ;}
}
}
cout << aim ;
} #include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
cin.ignore();
string s;
getline(cin, s);
while (m--) {
int l, r;
char c1, c2;
cin >> l >> r >> c1 >> c2;
for (int i = 0; i < n; ++i) {
if (i >= l - 1 && i <= r - 1 && s[i] == c1) {
s[i] = c2;
}
}
}
cout << s << "\n";
return 0;
} #include <stdio.h>
int main() {
int n, m;
if (scanf("%d %d ", &n, &m) != 2 || 1 > n || n > 100 || 1 > m || m > 100) {
return 1;
}
char s[101];
fread(s, 1, n, stdin);
for (int i = 0; i < n; i += 1) {
if ('a' > s[i] || s[i] > 'z') {
return 1;
}
}
for (int i = 0; i < m; i += 1) {
int l, r;
char c1, c2;
if (scanf("%d %d %c %c", &l, &r, &c1, &c2) != 4 || 1 > l || l > r || r > n || 'a' > c1 || c1 > 'z' || 'a' > c2 || c2 > 'z') {
return 1;
}
for (int j = l - 1; j < r; j += 1) {
if (s[j] == c1) {
s[j] = c2;
}
}
}
fwrite(s, 1, n, stdout);
return 0;
} #include<stdio.h>
#include<string.h>
int main() {
int n, m;
char s[101];
scanf("%d %d", &n, &m);
getchar();
fgets(s, sizeof(s), stdin);
for (int i = 0; i < m; i++) {
int l, r;
char c1, c2;
scanf("%d %d %c %c", &l, &r, &c1, &c2);
for (int j = l - 1; j < r; j++) {
if (s[j] == c1)s[j] = c2;
}
}
printf("%s", s);
return 0;
}