Source code for submission s885

Go to diff to previous submission

bugs.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <ctype.h>
  6. #include <math.h>
  7.  
  8. static char input[2000007];
  9. static char text[2000007];
  10. static int stav[2000007];
  11.  
  12. static char vzor[1007];
  13. static int automat[1007];
  14.  
  15. int main() {
  16.  
  17. int n;
  18. while ( scanf("%d %s", &n, vzor) != EOF ) {
  19.  
  20.  
  21. int vzor_len = strlen(vzor);
  22. // printf("vzor je '%s'\n", vzor);
  23.  
  24. int z = 0;
  25. automat[0] = 0;
  26. automat[1] = 0;
  27.  
  28. int i;
  29. for ( i = 1; i < vzor_len; ++i ) {
  30. while ( z != 0 && vzor[z] != vzor[i] )
  31. z = automat[z];
  32. if ( vzor[z] == vzor[i] ) ++z;
  33. automat[i+1] = z;
  34. }
  35.  
  36. int ii;
  37. for ( ii = 0; ii < n; ++ii ) {
  38.  
  39. // while ( getchar() != '\n' );
  40. fgets(input, sizeof(input), stdin);
  41.  
  42. int pos = -1;
  43. int text_len = strlen(input);
  44.  
  45. for ( i = 0; i < text_len; ++i )
  46. stav[i] = -1;
  47.  
  48. z = 0;
  49.  
  50. for ( i = 0; i < text_len; ++i ) {
  51.  
  52. if ( pos != -1 && stav[pos] != -1 )
  53. z = stav[pos];
  54.  
  55. // printf("write %d %c\n", pos+1, input[i]);
  56. text[++pos] = input[i];
  57. text[pos+1] = 0;
  58.  
  59. while ( z != 0 && vzor[z] != text[pos] )
  60. z = automat[z];
  61.  
  62. if ( vzor[z] == text[pos] )
  63. ++z;
  64.  
  65. stav[pos] = z;
  66. stav[pos+1] = -1;
  67.  
  68. if ( z == vzor_len ) {
  69. // printf("delete %d == %d\n", z, vzor_len);
  70. // printf("NULL on %d\n", pos-z+1);
  71. text[pos-z+1] = 0;
  72. pos = pos-z;
  73. // printf("new pos %d\n", pos);
  74. }
  75.  
  76. }
  77.  
  78. printf("%s", text);
  79.  
  80. }
  81.  
  82. }
  83.  
  84. return 0;
  85.  
  86. }
  87.  

Diff to submission s875

bugs.c

--- c4.s875.cteam004.bugs.c.0.bugs.c
+++ c4.s885.cteam004.bugs.c.0.bugs.c
@@ -28,5 +28,5 @@
 
                 int i;
-                for ( i = 1; i < vzor_len-1; ++i ) {
+                for ( i = 1; i < vzor_len; ++i ) {
                         while ( z != 0 && vzor[z] != vzor[i] )
                                 z = automat[z];