https://www.acmicpc.net/problem/11718

1
2
3
4
5
6
7
8
9
10
11
import java.util.Scanner;
 
public class Main{
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       while(sc.hasNextLine()){
              System.out.println(sc.nextLine());
       }
       sc.close();
   }
}
cs


https://www.acmicpc.net/problem/9012


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
#include <stack>
#include <stdio.h>
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    int N;
    unsigned long long count = 0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        stack<char> stack1;
        string input;
        cin >> input;
        while (!stack1.empty())
            stack1.pop();
        for (int i = 0; i < input.size(); i++) {
            if (stack1.empty())
                stack1.push(input[i]);
            else {
                if (input[i] == '(')
                    stack1.push(input[i]);
                else if (input[i] == ')')
                    if (stack1.top() == '(')
                        stack1.pop();
                    else
                        stack1.push(input[i]);
            }
        }
        if (stack1.empty())
            cout << "YES" << endl;
        else
            cout << "NO" << endl;
    }
    return 0;
}
cs


https://www.acmicpc.net/problem/3986


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
#include <stack>
#include <stdio.h>
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    int N;
    unsigned long long count = 0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        stack<char> stack1;
        string input;
        cin >> input;
        for (int i = 0; i < input.size(); i++) {
            // method 1
            if (!stack1.empty() && input[i] == stack1.top())
                stack1.pop();
            else
                stack1.push(input[i]);
            /* method 2
            if (stack1.empty())
                stack1.push(input[i]);
            else {
                if (input[i] == stack1.top())
                    stack1.pop();
                else
                    stack1.push(input[i]);
            }
            */
        }
        if (stack1.empty())
            count++;
    }
    cout << count;
    return 0;
}
cs


https://www.acmicpc.net/problem/6198


O(2n)



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
#include <stack>
#include <stdio.h>
#include <iostream>
 
using namespace std;
 
int main() {
    stack<int> stack1;
    int N;
    unsigned long long count = 0;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int input;
        cin >> input;
        if (stack1.empty())
            stack1.push(input);
        else {
            while (stack1.top() <= input) {
                stack1.pop();
                if (stack1.empty()) {
                    stack1.push(input);
                    break;
                }
            }
            if (stack1.top() > input)
                stack1.push(input);
        }
        count += stack1.size() - 1;
    }
    cout << count;
    return 0;
}
cs


+ Recent posts