Go to diff to previous submission
#include <cstdio> #include <cstring> #include <stack> using namespace std; #define NULLCHAR 2 void del(char LINE[], int begin, int end) { for (int i = begin; i <= end; i++) { LINE[i]=NULLCHAR; } } int main() { int N; char B[1010]; char LINE[2000010]; while (scanf("%d %1005s", &N, B) == 2) { gets(LINE); int B_len = strlen(B); for (int row = 0; row < N; row++) { gets(LINE); int index = 0, tmp; stack<int> sb, sc; while (LINE[index] != '\0') { if (LINE[index]==B[0]) { sb.push(index); sc.push(1); } else { if (sc.empty()) { index++; continue; } if (LINE[index]==B[sc.top()]) { tmp=sc.top(); sc.pop(); if (tmp+1==B_len) { del(LINE, sb.top(), index); sb.pop(); } else { sc.push(tmp+1); } } else { while (!sb.empty()) sb.pop(); while (!sc.empty()) sc.pop(); } } index++; } index = 0; while (LINE[index] != '\0'){ if (LINE[index] != NULLCHAR){ printf("%c", LINE[index]); } index++; } printf("\n"); } } return 0; }
--- c4.s1009.cteam032.bugs.cpp.0.bugs.cpp +++ c4.s1138.cteam032.bugs.cpp.0.bugs.cpp @@ -1,31 +1,17 @@ #include <cstdio> #include <cstring> +#include <stack> using namespace std; #define NULLCHAR 2 -inline bool match_delete(const int index, const int B_len, const char B[], char LINE[]){ - int b=0, l = index; - while (b < B_len){ - while (LINE[l] == NULLCHAR){ - l++; - } - - if(B[b] != LINE[l]){ - return false; - } - - b++; - l++; - } - - for (int i = index; i < l; i++){ - LINE[i] = NULLCHAR; +void del(char LINE[], int begin, int end) { + for (int i = begin; i <= end; i++) + { + LINE[i]=NULLCHAR; } - return true; } -int main() -{ +int main() { int N; char B[1010]; @@ -36,12 +22,31 @@ for (int row = 0; row < N; row++) { gets(LINE); - int index = 0; + int index = 0, tmp; + stack<int> sb, sc; while (LINE[index] != '\0') { - if (LINE[index] == B[0]){ - if (match_delete(index, B_len, B, LINE)){ - index -= B_len; - if (index < 0){ - index = 0; + if (LINE[index]==B[0]) { + sb.push(index); + sc.push(1); + } + else { + if (sc.empty()) { + index++; + continue; + } + if (LINE[index]==B[sc.top()]) { + tmp=sc.top(); + sc.pop(); + + if (tmp+1==B_len) { + del(LINE, sb.top(), index); + sb.pop(); } + else { + sc.push(tmp+1); + } + } + else { + while (!sb.empty()) sb.pop(); + while (!sc.empty()) sc.pop(); } }