description:
给出字符串s 将s中的子串a代替成b输出
solution:
所有字符串长度都有1e6 一开始采用O(n) substr寻找子串替换输出 T了一发
后面用 find 寻找子串a位置 删除a 插入b O(1)输出更新后的串即可
code:
#include <bits/stdc++.h> using namespace std; #define LL long long int main() { ios::sync_with_stdio(0); cin.tie(0),cout.tie(0); string s,a,b; cin >> s >> a >> b; int n = s.size(); int x = a.size(); while(1){ int pos = s.find(a); if(pos == string::npos){ break; } s.erase(pos,x); s.insert(pos,b); } cout << s << '\n'; return 0; }