Created
August 13, 2015 22:02
-
-
Save superjer/0c9698d6f3330a3537ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <memory.h> | |
#define MAX 1000 /* max input line length + 1 */ | |
static char line0[MAX]; /* the 2nd to last line of input */ | |
static char line1[MAX]; /* the last line of input */ | |
static int mark0[MAX]; /* markings for line0 */ | |
static int mark1[MAX]; /* markings for line1 */ | |
static int chain_count = 0; /* number of chains so far */ | |
static int high_chain = 0; /* highest chain mark so far */ | |
static void merge_chains(int a, int b) | |
{ | |
for( int i = 0; i < MAX; i++ ) | |
{ | |
if( mark0[i] == a ) | |
mark0[i] = b; | |
if( mark1[i] == a ) | |
mark1[i] = b; | |
} | |
} | |
static void set_mark(int i) | |
{ | |
int mark_up = mark0[i]; | |
int mark_left = i ? mark1[i-1] : 0; | |
if( mark_up && mark_left && mark_up != mark_left ) | |
{ | |
merge_chains(mark_left, mark_up); | |
mark1[i] = mark_up; | |
chain_count--; | |
} | |
else if( mark_up || mark_left ) | |
{ | |
mark1[i] = mark_up ? mark_up : mark_left; | |
} | |
else | |
{ | |
mark1[i] = ++high_chain; | |
chain_count++; | |
} | |
} | |
int main() | |
{ | |
while( !feof(stdin) ) | |
{ | |
memcpy(line0, line1, MAX * sizeof *line1); | |
memcpy(mark0, mark1, MAX * sizeof *mark1); | |
memset(line1, 0, MAX * sizeof *line0); | |
memset(mark1, 0, MAX * sizeof *mark1); | |
fgets(line1, MAX, stdin); | |
for( int i = 0; i < MAX; i++ ) | |
if( line1[i] == 'x' ) | |
set_mark(i); | |
} | |
printf("%d chain(s) found\n", chain_count); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment