uva : https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=320

 

BOJ : https://www.acmicpc.net/problem/14906

 

문제를 보면서 그대로 구현했는데

소스가 좀 더러울 수 있지만 최대한 정리해봤습니다.

 

재귀를 이용하여 문제를 해결했습니다.

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
#include <iostream>
#include <string>
using namespace std;
 
int slumps(string str, int idx, bool si) {
    if (idx >= str.size()) return -1;
    if (si) {
        if (!(str[idx] == 'D' || str[idx] == 'E')) return -1;
        if (idx + 1 < str.size() && str[idx + 1== 'F'return slumps(str, idx + 2false);
    }
    else {
        if (str[idx] == 'F'return slumps(str, idx + 1false);
        if (str[idx] == 'G' && str[idx - 1== 'F'return idx;
        if (str[idx] == 'D' || str[idx] == 'E'return slumps(str, idx, true);
    }
    return -1;
}
 
int slimps(string str, int idx, bool si) {
    if (idx >= str.size()) return -1;
    if (si) {
        if (str[idx] != 'A'return -1;
        if (idx + 1 < str.size()) {
            if (str[idx + 1== 'H'return idx + 1;
            else return slimps(str, idx + 1false);
        }
    }
    else {
        if (str[idx] == 'B') {
            int res = slimps(str, idx + 1true);
            if (res != -1 && str[res + 1== 'C'return res + 1;
        }
        if (str[idx] == 'D' || str[idx] == 'E') {
            int res = slumps(str, idx, true);
            if (res != -1 && str[res + 1== 'C'return res + 1;
        }
    }
    return -1;
}
 
bool solve(string str) {
    int idx = slimps(str, 0true);
    if (idx == -1return false;
    idx = slumps(str, idx + 1true);
    return idx == str.size() - 1 ? true : false;
}
 
int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
 
    cout << "SLURPYS OUTPUT\n";
    int tc;
    cin >> tc;
    while (tc--) {
        string str;
        cin >> str;
        if (solve(str)) cout << "YES\n";
        else cout << "NO\n";
    }
    cout << "END OF OUTPUT";
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter

'algorithm > uva' 카테고리의 다른 글

uva 10205번 stack 'em up  (0) 2020.04.27
uva 11994번 Happy Painting!  (0) 2020.04.06
uva 10150번 Doublets  (0) 2020.03.27
uva 10469번 To Carry or not to Carry  (0) 2020.03.23
uva 10010번 Where's Waldorf?  (0) 2020.03.23

+ Recent posts