[Ybtoj high-efficiency advanced 2.1] [string] word replacement

[Ybtoj high-efficiency advanced 2.1] [string] word replacement

topic

Insert picture description here
Insert picture description here


Problem-solving ideas

Add a space before the string s
so that there is a space before and after each word.
Add spaces before and after the words a and b
. Find and replace in the string s.
I don’t know why 90, can I have a look at it


Code

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int l;
string x, y, s;
int main() {
    
    
    getline(cin, s);
    cin >> x >> y;
    s = " " + s;
    x = " " + x + " ";
    y = " " + y + " ";
    l = x.size();
    int i = 0;
    while (s.find(x, i) != -1) {
    
    
        int xx = s.find(x, i);
        s.erase(xx, l);
        s.insert(xx, y);
        i++;
    }
    for (int i = 1; i < s.size(); i++) cout << s[i];
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45621109/article/details/113357838