A. Scarborough Fair

本文介绍了一个字符串操作的问题,其中主角Willem需要在朋友Grick的帮助下,对一个初始字符串进行多次替换操作。每次操作包括将指定范围内的某个字符替换成另一个字符。文章详细描述了输入输出格式及示例,提供了一个C++代码实现,展示了如何通过循环遍历字符串并执行替换操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Willem is taking the girl to the highest building in island No.28, however, neither of them knows how to get there.
Willem asks his friend, Grick for directions, Grick helped them, and gave them a task.
Although the girl wants to help, Willem insists on doing it by himself.
Grick gave Willem a string of length n.
Willem needs to do m operations, each operation has four parameters l, r, c1, c2, which means that all symbols c1 in range [l, r] (from l-th to r-th, including l and r) are changed into c2. String is 1-indexed.
Grick wants to know the final string after all the m operations.

Input
The first line contains two integers n and m (1 ≤ n, m ≤ 100).

The second line contains a string s of length n, consisting of lowercase English letters.

Each of the next m lines contains four parameters l, r, c1, c2 (1 ≤ l ≤ r ≤ n, c1, c2 are lowercase English letters), separated by space.

Output
Output string s after performing m operations described above.

Examples
inputCopy
3 1
ioi
1 1 i n
outputCopy
noi
inputCopy
5 3
wxhak
3 3 h x
1 5 x a
1 3 w g
outputCopy
gaaak
Note
For the second example:
After the first operation, the string is wxxak.
After the second operation, the string is waaak.
After the third operation, the string is gaaak.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n, m;
    while(cin >> n >> m)
    {
        string s;
        cin >> s;
        for(int i = 0; i < m; ++i)
        {
            int L, R;
            char fro, bac;
            cin >> L >> R >> fro >> bac;
            for(int j = L - 1; j <= R - 1; ++j)
                if(s[j] == fro )
                    s[j] = bac;
        }
        cout << s << '\n';
    }
    return 0;
}

### 使用 `split()` 方法分割字符串 在 JavaScript 中,`split()` 是一种非常常用的方法,用于将一个字符串按照指定的分隔符划分为多个子字符串,并将其存储在一个数组中[^1]。此方法不会修改原字符串的内容,而是返回一个新的数组。 以下是关于 `split()` 的具体说明: #### 基本语法 ```javascript string.split(separator, limit); ``` - **`separator`**: 这是一个可选参数,表示用来区分各个子字符串的标准。它可以是任何字符或者正则表达式。 - **`limit`**: 可选参数,定义了返回数组的最大长度。如果设置了该参数,则返回的数组长度不超过它规定的数量[^2]。 #### 示例代码 以下是一些常见的使用场景及其对应的实现方式: ##### 场景一:按空格分割字符串 当需要以空格作为分隔符时,可以这样写: ```javascript const str = 'Begonia flower sleepless'; const result = str.split(' '); console.log(result); // 输出 ["Begonia", "flower", "sleepless"] ``` 这里的结果会得到由单词组成的数组。 ##### 场景二:逐字拆解字符串 若希望获取到每一个单独的字符,可以通过传递空字符串 (`''`) 来完成这一目标: ```javascript var str = "Are you going to Scarborough Fair?"; document.write(str.split("")); // 结果类似于["A","r","e"," ","y","o","u"...] ``` 需要注意的是,在这种情况下,每个字母甚至包括空白都会被当作独立项处理[^3]。 ##### 场景三:无分隔符情况下的行为分析 如果没有提供具体的分隔符,默认整个输入会被视为单一单元并封装至含有单元素的数组里边去: ```javascript let rows= "aa,bb,cc"; let str = rows.split(); console.log(str); // 输出 ["aa,bb,cc"] ``` 这表明即使未明确指出如何切割数据源,最终依旧能够获得预期形式的数据结构——即保持原有状态不变的同时转化为数组类型[^4]。 ##### 场景四:限定结果集大小 有时候我们可能只需要前几个片段的信息就够了,这时候就可以利用第二个参数控制输出规模: ```javascript const str = "pink,red,blue"; const limitedArr = str.split(',', 2); console.log(limitedArr); // 返回 ["pink", "red"],因为只取前面两个项目 ``` 上述实例展示了如何通过设置上限值来限制所提取出来的条目数目[^5]。 ### 总结 综上所述,`split()` 函数提供了灵活多样的选项让用户可以根据实际需求自定义解析逻辑;无论是简单的词组分离还是复杂的模式匹配都可以轻松应对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值