Go to diff to previous submission
#include <stdlib.h> #include <stdio.h> #include <string.h> #define MAX(x,y) (((x)>=(y))?(x):(y)) #define MIN(x,y) (((x)<=(y))?(x):(y)) #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, line_len; char bug[MAX_BUG_LEN+5]; char *bug_ptr, *line_end, *p; /*DEBUG_PRINT("bug is %s\n", bug);*/ /* process lines */ for (i = 0; i < line_count; ++i) { line_end = line + line_len; /*DEBUG_PRINT("processing line %s", line);*/ p = line; /*DEBUG_PRINT("replace '%s' with '%s'\n", bug_ptr, bug_ptr+bug_len);*/ /*DEBUG_PRINT("line now: '%s'\n", line);*/ line_len -= bug_len; p = MAX(line, bug_ptr-bug_len); } } } return 0; }
--- c4.s748.cteam007.bugs.c.0.bugs.c +++ c4.s897.cteam007.bugs.c.0.bugs.c @@ -2,4 +2,7 @@ #include <stdio.h> #include <string.h> +#define MAX(x,y) (((x)>=(y))?(x):(y)) +#define MIN(x,y) (((x)<=(y))?(x):(y)) + #define MAX_BUG_LEN 100 @@ -12,70 +15,32 @@ 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; + int line_count, i, bug_len, line_len; + char bug[MAX_BUG_LEN+5]; + char *line = (char*)malloc(MAX_LINE_LEN+20); + char *bug_ptr, *line_end, *p; while (scanf("%d %s ", &line_count, bug) == 2) { bug_len = strlen(bug); - /* 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);*/ + /*DEBUG_PRINT("bug is %s\n", bug);*/ /* 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);*/ - printf("%s", line_ptr); - 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; + line_len = strlen(line); + line_end = line + line_len; + /*DEBUG_PRINT("processing line %s", line);*/ + p = line; + while ( (bug_ptr = strstr(line, bug)) ) { + /*DEBUG_PRINT("replace '%s' with '%s'\n", bug_ptr, bug_ptr+bug_len);*/ + memmove(bug_ptr, bug_ptr+bug_len, line_len - ((bug_ptr + bug_len) - line)+2); + /*DEBUG_PRINT("line now: '%s'\n", line);*/ + line_len -= bug_len; + p = MAX(line, bug_ptr-bug_len); } - /* vypis za poslednim bugem */ - printf("%s", line_ptr); - + printf("%s", line); } } free(line); - free(stack); return 0; }