Source code for submission s1203

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 counter = 0;
  17. int sp = 0;
  18. int dp = 0;
  19. int tp = 0;
  20. for (sp = 0; true; sp++, counter--) {
  21. if (!t[tp]) {
  22. dp -= tp;
  23. tp = 0;
  24. result = true;
  25. counter = tl;
  26. }
  27. if (!s[sp])
  28. break;
  29. if (tp && s[sp] == t[tp])
  30. tp++;
  31. else
  32. tp = s[sp] == t[0];
  33. d[dp++] = s[sp];
  34.  
  35. if (counter >= 0 && s[sp] == end && dp >= tl) {
  36. int i;
  37. for (i = 0; i < tl; i++) {
  38. if (d[dp-tl+i] != t[i])
  39. break;
  40. }
  41. if (i == tl)
  42. dp -= tl;
  43. }
  44.  
  45. }
  46. d[dp] = 0;
  47. return result;
  48. }
  49.  
  50. int main(int argc, char** argv) {
  51.  
  52. int lines;
  53. while (scanf("%d %s", &lines, tbuf) == 2) {
  54.  
  55. getchar();
  56.  
  57. for (int i = 0; i < lines; i++) {
  58.  
  59. cin.getline(buf[0], 2000001);
  60. cutBugs(buf[1], buf[0], tbuf);
  61. cout << buf[1] << endl;
  62.  
  63.  
  64. }
  65. }
  66.  
  67. return 0;
  68. }
  69.  
  70.  

Diff to submission s1178

bugs.cpp

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