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 sp = 0; int dp = 0; int tp = 0; for (sp = 0; s[sp]; sp++) { d[dp++] = s[sp]; if (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.s1064.cteam041.bugs.cpp.0.bugs.cpp +++ c4.s1178.cteam041.bugs.cpp.0.bugs.cpp @@ -11,4 +11,6 @@ bool cutBugs(char *d, const char *s, const char *t) { + int tl = strlen(t); + char end = t[tl-1]; bool result = false; int sp = 0; @@ -16,19 +18,14 @@ int tp = 0; for (sp = 0; s[sp]; sp++) { - if (!t[tp]) { - dp -= tp; - tp = 0; - result = true; - } - if (tp && s[sp] == t[tp]) - tp++; - else - tp = s[sp] == t[0]; d[dp++] = s[sp]; - } - if (!t[tp]) { - dp -= tp; - tp = 0; - result = true; + if (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; @@ -46,8 +43,6 @@ cin.getline(buf[0], 2000001); - - for (bool a = false; cutBugs(buf[!a], buf[a], tbuf); a = !a); - - cout << buf[0] << endl; + cutBugs(buf[1], buf[0], tbuf); + cout << buf[1] << endl;