Skip to content

Instantly share code, notes, and snippets.

@michalwa
Last active February 16, 2020 19:49
Show Gist options
  • Save michalwa/fdb50c0c0eba0896792b2a4192f92063 to your computer and use it in GitHub Desktop.
Save michalwa/fdb50c0c0eba0896792b2a4192f92063 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
char *gets(char *str);
int main() {
size_t n, i;
if (!scanf("%zd\n", &n)) return 1;
char *stack = malloc(250 * sizeof(char)); // stack array pointer
size_t size = 0; // stack size
char *str = malloc(251 * sizeof(char)); // gets() buffer
for (i = 0; i < n; i++) {
if (!gets(str)) return 1;
char *c = str;
bool valid = true;
do {
switch (*c) {
case '{': case '[': case '(':
stack[size++] = *c; break;
case '}': case ']': case ')':
if((valid = (size > 0 && stack[size - 1] == (*c == ')' ? '(' : *c - 2)))) size--;
}
} while(*++c && valid);
valid = valid & (size == 0);
printf("%s\n", valid ? "TAK" : "NIE");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment