Help in USACO Bronze 2015 Censoring

Problem Info

Censoring

My Work

#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define debug(x) cout<<":["<<x<<"XE]"<<endl;
#define debug2(x,y) cout<<":["<<x<<" "<<y<<"XE]"<<endl;
#define _ ios_base::sync_with_stdio(false);
#define mod 1000000007
#define mod2 998244353



int main()
{_
    ll t=1,ca=1;
    //cin>>t;
    while(t--){
        freopen("censor.in","r",stdin);
        freopen("censor.out","w",stdout);
        string s,tt,s2;
        cin>>s>>tt;
        ll n=tt.size();
        if(n==1){
            for(int i=0;i<s.size();i++){
                if(s[i]!=tt[0])cout<<s[i];
            }
            continue;
        }
        for(int i=0;i<s.size();i++){
            if(i>=n-1&&s.substr(i-n+1,n)==tt){
                for(int j=1;j<=n;j++){
                    s.erase(i,1);
                    i--;
                }
            }
        }
        cout<<s<<endl;
    }
}

I have iterated the string and if found the last T string length substring matched with string T then erased that substring and continued this way…

Question

This solution is giving TLE, I have downloaded test data but failed to debug because of big test data and I don’t see any reason to TLE of my solution. Can anyone please help why my solution is TLE or give a small test data that cause TLE?

What do you mean by this?

Very big strings in test cases and I cannot figure out where is the problem happening…

Didn’t you say the problem was a TLE, not a WA?

Yes.

Try looking at how many operations your code is performing. Perhaps the s.subtr is taking up a large portion of the runtime, as it runs in \mathcal{O}(n).

I cannot figure out. By taking a new string and adding each character of actual string and doing same thing i.e. substr and erase on new string (as official analysis) works fine and got AC but doing that on existing string gives TLE :disappointed:
@SansPapyrus683

It looks like you’re slicing in the middle of the string, which takes much longer than just cutting off the end of the string.