2024-05-21 01:09:29

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System.IO;
using System.Text;
using System.Linq;
using System.Reflection.PortableExecutable;
using System.Security.Cryptography;
 
namespace 연습장
{
    internal class Program
    {
        static void Main(string[] args)
        {
            using var reader = new StreamReader(Console.OpenStandardInput());
            using var print = new StreamWriter(Console.OpenStandardOutput());
 
            _10799_boj boj = new _10799_boj();
 
            boj.boj_10799();
 
        }
    }
        internal class _10799_boj
    {
        public void boj_10799()
        {
            string sticks = Console.ReadLine();
            Stack stack = new Stack();
            
            stack.stack = new string[sticks.Length];
 
            int result = 0;
 
            for (int i = 0; i < sticks.Length; i++)
            {
                if (sticks[i] == '(')
                {
                    stack.Push("(");
                    continue;
                }
                if (sticks[i] == ')' && i >= 1)
                {
                    if (sticks[i - 1== '(')
                    {
                        stack.Pop();
                        result += stack.size;
                        continue;
                    }
                    else
                    {
                        stack.Pop();
                        result++;
                        continue;
                    }
                }
            }
            Console.WriteLine(result);
        }
    }
    public class Stack
    {
        public int size;
        public string[] stack;
        public int top = -1;
        public void Push(string data)
        {
            if (IsStackFull())
                return;
            stack[++top] = data;
            size++;
        }
        public string Pop()
        {
            string res = null;
            if (IsStackEmpty())
                return res;
            res = stack[top--];
            size--;
            return res;
        }
        public bool IsStackFull()
        {
            bool res = false;
            if (top == stack.Length - 1)
                res = true;
            return res;
        }
        public bool IsStackEmpty()
        {
            bool res = false;
            if (top == -1)
                res = true;
            return res;
        }
 
    }
}
 
 
cs

 

입력받은 문자열을 하나씩 되짚어가며

여는 괄호 " ( " 를 만나면 일단 스택에 저장한다.

이후 닫는 괄호 " ) " 를 만나게 되면       (0번 인덱스에 오는 닫는 괄호는 의미 없으니 거르는 코드를 추가해 주었다)

바로 직전 괄호가 여는 괄호인 경우 : 스택에서 해당 괄호를 제거 해 주고 스택에 남은 여는 괄호만큼 결과값에 추가

직전 괄호가 여는 괄호가 아닐 경우 : 스택에서 가장 앞에 있는 여는 괄호를 제거해주고, 결과값에 1 추가

이런식으로 진행해 주었더니 한번에 정답을 얻을 수 있었다

 

 

'하루 한 접시' 카테고리의 다른 글

[백준] 17298번 : 오큰수 [C#]  (0) 2024.05.22
[백준] 6198번 : 옥상 정원 꾸미기 [C#]  (0) 2024.05.22
[백준] 10845번 : 큐 [C#]  (0) 2024.05.21
[백준] 28279번: 덱 2 [C#]  (0) 2024.05.20
[백준] 2493번 : 탑 [C#]  (0) 2024.05.20