AC
ac的代码,熟悉string的性质,而我太菜了,要加油啊。。
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
string s;
string t;
int main(){
cin >> s;
cin >> t;
int len = t.size();
for(int i = 0; i < s.size(); i++){
if(s.substr(i,len) == t){
s.erase(i,len);
i = max(0,i - len);
}
}
cout << s << endl;
return 0;
}
TLE (6/15)
估计是重复find查找,加上while循环导致TLE.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
string s;
string t;
int main(){
cin >> s;
cin >> t;
int len = t.size();
while(s.find(t) != -1){
int lens = s.size();
int pos = s.find(t);
string str(s,0,pos);
// cout << "str = " << str << endl;
string tri(s,pos + len,lens - 1);
// cout <<"tri = " << tri << endl;
s = str + tri;
}
cout << s << endl;
return 0;
}