본문 바로가기

PS

[ 백준 / C++ ] 12904 : A와 B

[ 문제 ]

 

12904번: A와 B (acmicpc.net)

 

[ 접근방법 ]

 

문자열 T의 맨 뒷 문자를 보면서 로직을 진행한다.

 

만약 맨 뒷 문자가 A라면, A 앞의 부분문자열은 뒤집지 말고 그대로 S와 비교해야 한다.

만약 맨 뒷 문자가 B라면, B 앞의 부분문자열은 뒤집고 S와 비교해야 한다.

 

부분문자열을 T로 바꾸고 S와 비교했을 때 문자열의 길이가 같다면 비교하면 되고, 아니라면

위 로직을 반복하면 된다.

 

[ 소스코드 ]

 

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string s, t;
int ans = 0;

void f(string x, string y)
{
    if (ans)
        return;
    if (x.length() == y.length())
    {
        if (x == y)
            ans = 1;
        return;
    }

    string str = y.substr(0, y.length() - 1);

    if (y[y.length() - 1] == 'A')
        f(x, str);
    else
    {
        reverse(str.begin(), str.end());
        f(x, str);
    }

    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> s >> t;

    f(s, t);

    cout << ans;

    return 0;
}

'PS' 카테고리의 다른 글

[ 백준 / C++ ] 1011 : Fly me to the Alpha Centauri  (0) 2024.06.13
[ 백준 / C++ ] 5430 : AC  (0) 2024.06.12
[ 백준 / C++ ] 14490 : 백대열  (0) 2024.06.10
[ 백준 / C++ ] 15486 : 퇴사 2  (0) 2024.06.05
[ 백준 / C++ ] 1024 : 수열의 합  (0) 2024.06.05