Source code for submission s1178

Go to diff to previous submission

bugs.cpp

  1.  
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. using namespace std;
  8.  
  9. char buf[2][2000002];
  10. char tbuf[1002];
  11.  
  12. bool cutBugs(char *d, const char *s, const char *t) {
  13. int tl = strlen(t);
  14. char end = t[tl-1];
  15. bool result = false;
  16. int sp = 0;
  17. int dp = 0;
  18. int tp = 0;
  19. for (sp = 0; s[sp]; sp++) {
  20. d[dp++] = s[sp];
  21. if (s[sp] == end && dp >= tl) {
  22. int i;
  23. for (i = 0; i < tl; i++) {
  24. if (d[dp-tl+i] != t[i])
  25. break;
  26. }
  27. if (i == tl)
  28. dp -= tl;
  29. }
  30. }
  31. d[dp] = 0;
  32. return result;
  33. }
  34.  
  35. int main(int argc, char** argv) {
  36.  
  37. int lines;
  38. while (scanf("%d %s", &lines, tbuf) == 2) {
  39.  
  40. getchar();
  41.  
  42. for (int i = 0; i < lines; i++) {
  43.  
  44. cin.getline(buf[0], 2000001);
  45. cutBugs(buf[1], buf[0], tbuf);
  46. cout << buf[1] << endl;
  47.  
  48.  
  49. }
  50. }
  51.  
  52. return 0;
  53. }
  54.  
  55.  

Diff to submission s1064

bugs.cpp

--- 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;