40 lines
983 B
C
40 lines
983 B
C
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
|
|
int main(int argc, char **argv) {
|
|
int length, maxlength, stacksize;
|
|
|
|
length = maxlength = stacksize = 0;
|
|
|
|
while (true) {
|
|
int c;
|
|
c = getc(stdin);
|
|
if (c == '\n') {
|
|
/* Remove extra left parenthesis and check if it's the longest substring. */
|
|
if (length - stacksize > maxlength) {
|
|
maxlength = length - stacksize;
|
|
}
|
|
|
|
printf("%d\n", maxlength);
|
|
length = maxlength = stacksize = 0;
|
|
} else if (c == EOF) {
|
|
putchar('\n');
|
|
break;
|
|
} else if (c == '(') {
|
|
length++;
|
|
stacksize++;
|
|
} else if (c == ')') {
|
|
stacksize--;
|
|
length++;
|
|
if (stacksize == 0 && length > maxlength) {
|
|
/* Parenthesis substring completely closed, keep length and continue counting. */
|
|
maxlength = length;
|
|
} else if (stacksize < 0) {
|
|
/* Extra closing parenthesis, reset all counters. */
|
|
length = 0;
|
|
stacksize = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|