티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in Inflearn


#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve

  1. 예전에 stack이 아닌 다른 방법으로 풀었던 문제네!?

     단순하게 '(' 입력되면 stack 에 push 해주고,

     ')'이 입력되면 pop 해주면 된다.

     여기서! 만약에 pop 을 해주려고 하는데 stack 이 empty 상태라면 No 가 될 것이다.

   

    모든 입력을 탐색한 결과

    empty() 가 True 라면 결과는 YES 가 될 것이다.

    FALSE 라면 NO 가 되겠쥬?


#. Code

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
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
 
using namespace std;
 
char str[30];
 
int main(void)
{
    freopen("input.txt""rt", stdin);
 
    int i;
    stack<int> s;
 
    scanf("%s"&str);
 
    for (i = 0; str[i] != '\0'; i++)
    {
        if (str[i] == '(')
            s.push(str[i]);
        else
        {
            if (s.empty())
            {
                printf("NO\n");
 
                return 0;
            }
            else
                s.pop();
        }
    }
 
    if (s.empty())
        printf("YES\n");
    else
        printf("NO\n");
 
    return 0;
}
cs


#. Other code

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
#include<stdio.h>
#include<stack>
#include<algorithm>
 
using namespace std;
 
int main(){
    stack<char> s;
    char a[50];
    int i, flag=1;
 
    scanf("%s"&a);
 
    for(i=0; a[i]!='\0'; i++){
        if(a[i]=='(') s.push(a[i]);
        else{
            if(s.empty()){
                printf("NO\n");
                flag=0;
 
                break;
            }
            else s.pop();
        }
    }
 
    if(s.empty() && flag==1
        printf("YES\n");
    else if(!s.empty() && flag==1)
        printf("NO\n");
 
    return 0;
}
cs

return 0 사용과 falg 사용에 대한 차이 빼고는 역시 강사님과 동일하게 구현했다 Yo~~!


#. Result

  - Input --------------------------------------------------------

(()(()))(()

()()(()())

------------------------------------------------------------------


  - Output --------------------------------------------------------

NO

YES

------------------------------------------------------------------



반응형

'PS > Problem_Solving' 카테고리의 다른 글

[Inflearn] 재귀함수 이진수 출력  (0) 2020.04.29
[Inflearn] 기차운행  (0) 2020.04.29
[Inflearn] K진수 출력  (0) 2020.04.29
[Inflearn] 블록의 최댓값  (0) 2020.04.28
[Inflearn] 각 행의 평균과 가장 가까운 값  (0) 2020.04.28
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday