알고리즘/SW Expert Academy

[D4] 5432. 쇠막대기 자르기

혬혬 2019. 10. 6. 10:58
728x90
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

int main(void) {
	int test_case = 0;
	char buffer = 0;
	scanf("%d", &test_case);
	scanf("%c", &buffer);
	for (int i = 0; i < test_case; i++) {
		char words[100010] = { 0 };
		for (int j=0;;j++) {
			scanf("%c", &words[j]);
			if (words[j] == '\n')
				break;
		}
		char before = 0;
		int open = 0;
		int number = 0;
		for (int j = 0;; j++) {
			if (words[j] == '\n')
				break;
			if (words[j] == '(') {
				open++;
			}
			else if (words[j] == ')') {
				if (before == '(') {
					open--;
					number += open;
				}
				else {
					number++;
					open--;
				}
			}
			before = words[j];
		}
		printf("#%d %d\n",i+1, number);
	}
	return 0;
}
728x90