Go to diff to previous submission
#include <stdio.h> #include <iostream> #include <stdlib.h> #include <string.h> using namespace std; char buf[2][2000002]; char tbuf[1002]; bool cutBugs(char *d, const char *s, const char *t) { int tl = strlen(t); char end = t[tl-1]; bool result = false; int counter = 0; int sp = 0; int dp = 0; int tp = 0; for (sp = 0; true; sp++, counter--) { if (!t[tp]) { dp -= tp; tp = 0; result = true; counter = tl; } if (!s[sp]) break; if (tp && s[sp] == t[tp]) tp++; else tp = s[sp] == t[0]; d[dp++] = s[sp]; if (counter >= 0 && s[sp] == end && dp >= tl) { int i; for (i = 0; i < tl; i++) { if (d[dp-tl+i] != t[i]) break; } if (i == tl) dp -= tl; } } d[dp] = 0; return result; } int main(int argc, char** argv) { int lines; while (scanf("%d %s", &lines, tbuf) == 2) { getchar(); for (int i = 0; i < lines; i++) { cin.getline(buf[0], 2000001); cutBugs(buf[1], buf[0], tbuf); cout << buf[1] << endl; } } return 0; }
--- c4.s1178.cteam041.bugs.cpp.0.bugs.cpp +++ c4.s1203.cteam041.bugs.cpp.0.bugs.cpp @@ -14,10 +14,24 @@ char end = t[tl-1]; bool result = false; + int counter = 0; int sp = 0; int dp = 0; int tp = 0; - for (sp = 0; s[sp]; sp++) { + for (sp = 0; true; sp++, counter--) { + if (!t[tp]) { + dp -= tp; + tp = 0; + result = true; + counter = tl; + } + if (!s[sp]) + break; + if (tp && s[sp] == t[tp]) + tp++; + else + tp = s[sp] == t[0]; d[dp++] = s[sp]; - if (s[sp] == end && dp >= tl) { + + if (counter >= 0 && s[sp] == end && dp >= tl) { int i; for (i = 0; i < tl; i++) { @@ -28,4 +42,5 @@ dp -= tl; } + } d[dp] = 0;