아이디어는 스택에 문자열을 하나씩 받아오면서 문자열의 끝자리가 폭발 문자열의 끝자리와 같으면

폭발 가능성을 검사한다.

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 <iostream>
#include <string>
#include <stdio.h>
 
#pragma warning (disable : 4996)
using namespace std;
 
int main() {
    string str, bomb;
    char stack1[1000001];
    cin >> str >> bomb;
 
    int index = 0;
    int strlen = str.length();
    int bomblen = bomb.length();
 
    for(int i = 0; i < strlen; i++){
        stack1[index++= str[i];
        if (str[i] == bomb[bomblen - 1]) { //compare
            bool check = false;
            for (int j = bomblen - 1, k = index - 1; j >= 0; j--, k--)
                if (bomb[j] != stack1[k]) {
                    check = true;
                    break;
                }
            if (!check) index -= bomblen;
        }
    }
 
    if (index != 0)
        for (int i = 0; i < index; i++)
            printf("%c", stack1[i]);
    else
        printf("FRULA");
 
    return 0;
}
cs


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


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
#include <iostream>
#include <stdio.h>
#include <deque>
 
#pragma warning (disable : 4996)
using namespace std;
 
int main() {
    deque<int> deq;
    int N, M;
    cin >> N >> M;
    for (int i = 1; i <= N; i++)
        deq.push_back(i);
    printf("<");
    int del;
    while (!deq.empty()) {
        for (int i = 0; i < M; i++) {
            del = deq.front();
            deq.pop_front();
            if (i != M - 1)
                deq.push_back(del);
        }
        if (!deq.empty())
            printf("%d, ", del);
        else
            printf("%d", del);
    }
    printf(">");
 
    return 0;
}
cs


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


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
#include <iostream>
#include <cstring>
#include <string>
#include <stack>
 
#pragma warning (disable : 4996)
using namespace std;
 
int main() {
    int N;
    string input;
    stack<char> stack1, stack2;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> input;
        int size1 = input.size();
        for (int j = 0; j < size1; j++) {
            if (input[j] == '<') {
                if (stack1.empty() == 0){
                    char a;
                    a = stack1.top();
                    stack1.pop();
                    stack2.push(a);
                }
            }
            else if (input[j] == '>') {
                if (stack2.empty() == 0){
                    char a;
                    a = stack2.top();
                    stack2.pop();
                    stack1.push(a);
                }
            }
            else if (input[j] == '-') {
                if (stack1.empty() == 0)
                    stack1.pop();
            }
            else
                stack1.push(input[j]);
        }
 
        if (stack1.size() > 0){
            size1 = stack1.size();
            for (int j = 0; j < size1; j++) {
                char a;
                a = stack1.top();
                stack1.pop();
                stack2.push(a);
            }
        }
 
        int size = stack2.size();
        for (int j = 0; j < size; j++) {
            char a;
            a = stack2.top();
            stack2.pop();
            cout << a;
        }
 
        cout << endl;
    }
 
    return 0;
}
cs


+ Recent posts