题解 | #查找#

查找

https://www.nowcoder.com/practice/a988eda518f242c29009f8620f654ede

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

// 翻转函数
void reverse(string &str, int i, int len){
    for(int j = i; j <= (2 * i + len - 1) / 2; j++){
        char temp;
        temp = str[j];
        str[j] = str[2 * i + len - 1 - j];
        str[2 * i + len - 1 - j] = temp;
    }
    cout << str;
}
// 替换函数
void replace(string &str, int i, int len, string str_rep){
    int count = 0;
    string str_post = str.substr(i + len);
    string str_pre = str.substr(0, i);
    str = str_pre + str_rep + str_post;
    cout << str;
}
int main(){
    string str;
    int n;
    cin >> str >> n; // 输入字符串bac 和 命令的条数n
    for(int i = 0; i < n; i++){
        string order; // order变量存放命令
        cin >> order; // 输入命令003 、 112as
        if(order[0] == '0'){ // 翻转命令
            int i, len;
            reverse(str, order[1] - '0', order[2] - '0');
        }else{ // 替换命令
            string str_rep = order.substr(3); // str_rep存放用来替换的字符串内容
            replace(str, order[1] - '0', order[2] - '0', str_rep);
        }
        cout << endl;
    }
    return 0;
}

全部评论

相关推荐

头像
04-29 10:53
已编辑
东北大学 自动化类
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务