Source code for submission s915

Go to diff to previous submission

bugs.cpp

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int myFind ( int lastPos, int length, string & line, string & text, bool *& erased, bool & count )
  9. {
  10. int k = 0;
  11. int g;
  12. bool toErase = true;
  13. for ( int i = lastPos; i < (int)(length - text.length()); i++ )
  14. {
  15. if ( erased[i] == false && line[i] == text[0] )
  16. {
  17. k = 1;
  18. g = i + 1;
  19. for ( int j = i + 1; j < (int)(i + text.length()); j++ )
  20. {
  21. while ( erased[g] )
  22. g++;
  23. if ( g >= length || line[g] != text[k++] )
  24. {
  25. toErase = false;
  26. break;
  27. }
  28. g++;
  29. }
  30. if ( toErase )
  31. {
  32. k = i;
  33. for ( int z = i; z < i + (int)text.length(); z++ )
  34. {
  35. while ( erased[k] )
  36. k++;
  37. erased[k] = true;
  38. }
  39. count = true;
  40. return i;
  41. }
  42. toErase = true;
  43. }
  44. }
  45. return -1;
  46. }
  47.  
  48. void analyseLine ( string & text, string & line, bool * erased )
  49. {
  50. int length = (int) line.length();
  51. int lastPos = 0;
  52. bool count = false;
  53. if ( !length )
  54. {
  55. cout << endl;
  56. return;
  57. }
  58. while ( 1 )
  59. {
  60. lastPos = myFind ( lastPos, length, line, text, erased, count );
  61. if ( lastPos == -1 )
  62. break;
  63. lastPos = (int)lastPos - text.length() + 1 >= 0 ? lastPos - (int)text.length() + 1 : 0;
  64. }
  65. if ( !count )
  66. cout << line << endl;
  67. else
  68. {
  69. for ( int i = 0; i < length; i++ )
  70. {
  71. if ( !erased[i] )
  72. cout << line[i];
  73. }
  74. cout << endl;
  75. }
  76. }
  77.  
  78. int main ( void )
  79. {
  80. int n, i, j;
  81. char bug[1010];
  82. char tmp;
  83. bool * erased = new bool[2000100];
  84. string text, line;
  85.  
  86. while ( scanf ( "%d %s%c", &n, bug, &tmp ) == 3 )
  87. {
  88. text = string ( bug );
  89. for ( i = 0; i < n; i++ )
  90. {
  91. getline ( cin, line );
  92. for ( j = 0; j < (int)line.length(); j++ )
  93. erased[j] = false;
  94. analyseLine ( text, line, erased );
  95. }
  96. }
  97.  
  98. delete [] erased;
  99. return 0;
  100. }
  101.  

Diff to submission s869

bugs.cpp

--- c4.s869.cteam019.bugs.cpp.0.bugs.cpp
+++ c4.s915.cteam019.bugs.cpp.0.bugs.cpp
@@ -9,4 +9,5 @@
  {
    int k = 0;
+   int g;
    bool toErase = true; 
    for ( int i = lastPos; i < (int)(length - text.length()); i++ )
@@ -15,13 +16,15 @@
        {
          k = 1;
+         g = i + 1;
          for ( int j = i + 1; j < (int)(i + text.length()); j++ )
           {
-            while ( erased[j] )
-              j++;
-            if ( j >= length || line[j] != text[k++] )
+            while ( erased[g] )
+              g++;
+            if ( g >= length || line[g] != text[k++] )
              {
                toErase = false;
                break;
              }
+            g++; 
           }
          if ( toErase )
@@ -78,5 +81,5 @@
    char bug[1010];
    char tmp;
-   bool erased[2000010];
+   bool * erased = new bool[2000100];
    string text, line;
    
@@ -92,4 +95,6 @@
        } 
     }
+    
+   delete [] erased; 
    return 0;
  }