SWM(1218)

No Image;

1218 4일차 괄호 짝짓기

-> 해당 코드 문제. ((( 이런 식을 들어와도 무조건 1을 찍는다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;

public class Solution {

	private static int T = 10;

	public static void main(String[] args) throws Exception  {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		Stack<String> stack;
		int t = 1;
		while(T-- > 0) {
			stack = new Stack<String>();

			int num = Integer.parseInt(bf.readLine());

			String read_data = bf.readLine();
			String[] data = read_data.split("");
			boolean check = false;

			 for(int i = 0 ; i < num; i ++){
				 String temp = data[i];

				 if(data[i].equals("(") || data[i].equals("{") || data[i].equals("<") || data[i].equals("[")){
					 stack.push(temp);
				 }
				 else {
					if(stack.isEmpty()) {
						break;
					}

					if(data[i].equals(")")){
						 String s = stack.pop();
						 if(!s.equals("(")) {
							 break;
						 }
					 }
					 if(data[i].equals("]")){
						 String s = stack.pop();
						 if(!s.equals("[")) {
							 break;
						 }
					 }
					 if(data[i].equals(">")){
						 String s = stack.pop();
						 if(!s.equals("<")) {
							 break;
						 }
					 }
					 if(data[i].equals("}")){
						 String s = stack.pop();
						 if(!s.equals("{")) {
							 break;
						 }
					 }
				 }
				 if(i == num-1) {
					 check = true;
				 }
			 }

			 if(check == true) {
				 System.out.println("#" + t + " "+ "1");
			 }else {
				 System.out.println("#" + t + " "+ "0");
			 }
			 t++;


		}
	}


}


참고자료

SWE selfStudyBook(2)

0%