Source code for submission s1138

Go to diff to previous submission

bugs.cpp

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <stack>
  4. using namespace std;
  5.  
  6. #define NULLCHAR 2
  7.  
  8. void del(char LINE[], int begin, int end) {
  9. for (int i = begin; i <= end; i++)
  10. {
  11. LINE[i]=NULLCHAR;
  12. }
  13. }
  14.  
  15. int main() {
  16. int N;
  17. char B[1010];
  18. char LINE[2000010];
  19. while (scanf("%d %1005s", &N, B) == 2) {
  20. gets(LINE);
  21. int B_len = strlen(B);
  22. for (int row = 0; row < N; row++) {
  23. gets(LINE);
  24. int index = 0, tmp;
  25. stack<int> sb, sc;
  26. while (LINE[index] != '\0') {
  27. if (LINE[index]==B[0]) {
  28. sb.push(index);
  29. sc.push(1);
  30. }
  31. else {
  32. if (sc.empty()) {
  33. index++;
  34. continue;
  35. }
  36. if (LINE[index]==B[sc.top()]) {
  37. tmp=sc.top();
  38. sc.pop();
  39.  
  40. if (tmp+1==B_len) {
  41. del(LINE, sb.top(), index);
  42. sb.pop();
  43. }
  44. else {
  45. sc.push(tmp+1);
  46. }
  47. }
  48. else {
  49. while (!sb.empty()) sb.pop();
  50. while (!sc.empty()) sc.pop();
  51. }
  52. }
  53. index++;
  54. }
  55. index = 0;
  56. while (LINE[index] != '\0'){
  57. if (LINE[index] != NULLCHAR){
  58. printf("%c", LINE[index]);
  59. }
  60. index++;
  61. }
  62. printf("\n");
  63. }
  64. }
  65. return 0;
  66. }
  67.  

Diff to submission s1009

bugs.cpp

--- 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();
                                         }
                                 }