#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_BUG_LEN 100
#define MAX_LINE_LEN 2000000
/*
#define DEBUG_PRINT(x, ...) fprintf(stderr, x, __VA_ARGS__)
*/
int main(void)
{
int line_count, i, bug_len;
char bug[MAX_BUG_LEN+5], bug_reversed[MAX_BUG_LEN+5], bug_letters[27]; /*A-Z*/
char *line
= (char*)malloc(MAX_LINE_LEN
+5); char *stack
= (char*)malloc(MAX_LINE_LEN
+5); int stack_len;
char *line_ptr, *p;
char *maybe_bug_chunk, *bug_on_stack;
while (scanf("%d %s ", &line_count
, bug
) == 2) { /* conpute bug reverse */
for (i = 0; i < bug_len; ++i) {
bug_reversed[bug_len-i-1] = bug[i];
}
bug_reversed[bug_len] = '\0';
/* compute bug letters */
for (i = 0; i < 26; ++i) bug_letters[i] = '\0';
for (i = 0; i < bug_len; ++i) {
bug_letters[ bug[i] - 'A' ] = 1;
}
/* DEBUG_PRINT("bug is %s, reverse bug is %s, bug len is %d\n", bug, bug_reversed, bug_len);*/
/* process lines */
for (i = 0; i < line_count; ++i) {
fgets(line
, MAX_LINE_LEN
+5, stdin
); /* DEBUG_PRINT("processing line %s", line);*/
line_ptr = line;
while ( (maybe_bug_chunk
= strchr(line_ptr
, bug
[0])) ) { /* print until potential bug chunk */
maybe_bug_chunk[0] = '\0';
/* DEBUG_PRINT("found potential chunk at line[%d]\n", maybe_bug_chunk - line);*/
maybe_bug_chunk[0] = bug[0];
/* handle bug chunk */
p = maybe_bug_chunk;
stack_len = 0;
while (*p >= 'A' && *p <= 'Z' && bug_letters[*p - 'A']) {
stack[ stack_len++ ] = *p;
p++;
/* lze srovnat bug? */
if (stack_len >= bug_len) {
bug_on_stack = stack+stack_len-bug_len;
if (strncmp(bug
, bug_on_stack
, bug_len
) == 0) { /* srovnej bug */
stack_len -= bug_len;
}
}
}
/* vypis toho, co zbyva na zasobniku */
while (stack_len > 0) {
fputc( stack
[--stack_len
], stdout
); }
/* END handle bug chunk */
line_ptr = p;
}
/* vypis za poslednim bugem */
}
}
return 0;
}