bugs.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <math.h>
static char input[2000007];
static char text[2000007];
static int stav[2000007];
static char vzor[1007];
static int automat[1007];
int main() {
int n;
while ( scanf("%d %s", &n
, vzor
) != EOF
) {
// printf("vzor je '%s'\n", vzor);
int ii, i;
for ( ii = 0; ii < n; ++ii ) {
// while ( getchar() != '\n' );
fgets(input
, sizeof(input
), stdin
);
automat[0] = 0;
automat[1] = 0;
int z = 0;
int pos = -1;
for ( i = 0; i < text_len; ++i )
stav[i] = -1;
for ( i = 1; i < vzor_len-1; ++i ) {
while ( z != 0 && vzor[z] != vzor[i] )
z = automat[z];
if ( vzor[z] == vzor[i] ) ++z;
automat[i+1] = z;
}
z = 0;
for ( i = 0; i < text_len; ++i ) {
if ( pos != -1 && stav[pos] != -1 )
z = stav[pos];
// printf("write %d %c\n", pos+1, input[i]);
text[++pos] = input[i];
text[pos+1] = 0;
while ( z != 0 && vzor[z] != text[pos] )
z = automat[z];
if ( vzor[z] == text[pos] )
++z;
stav[pos] = z;
stav[pos+1] = -1;
if ( z == vzor_len ) {
// printf("delete %d == %d\n", z, vzor_len);
// printf("NULL on %d\n", pos-z+1);
text[pos-z+1] = 0;
pos = pos-z;
// printf("new pos %d\n", pos);
}
}
}
}
return 0;
}