AcWing 1883. 删减
原题链接
简单
作者:
绿水
,
2022-04-20 20:56:28
,
所有人可见
,
阅读 190
十分巧妙用一个字符串栈去实现边读边比较的过程
分析:边读边比较从栈顶往下的t个字符是否与t相等,
若相等,则直接把栈中的t弹出, 并继续比较到找不到栈定往下的t为止
1. size不够, 2. 不相等(可以推知前面的比较过了, 除了最后一个t,都不可能再相等)
若不等,继续读
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int main()
{
string s, t;
cin >> s >> t;
string stk;
for (auto c: s)
{
stk += c;
if (stk.size() > t.size() && stk.substr(stk.size() - t.size()) == t)
stk.erase(stk.end() - t.size(), stk.end());
}
cout << stk << endl;
return 0;
}