C++ 353347
Edits:
- Thanks to @algorithmshark for lots of help making the original shorter
- Thanks to @Adalynn for a good suggestion to save bytes
- Thanks to @ceilingcat for many, many very nice pieces of golfing - now even shorter
Here is the code:
#import<bits/stdc++.h>
#define N(x)o[i]);else if(n<x)o[i]==92?
#define O (o[++i]):o[i]==47?n++:
char p[198],*q=p,*s=p+99;int i,t;main(int n,char**m){for(std::string o=m[1];i<=o.size();++i){if(!N(3)putchar O putchar(N(4)*q++=O(*q++=N(5)*s++=O(*s++=o[i]);if(n>4){for(n=2;~(t=o.find(p,i+1));)o=o;o=o.substr(0,t)+(p+99)+&o[t+strlen+s+&o[t+strlen(p)];bzero])s=p+99;bzero(q=p,198);s=p+99;;}}}