🧩PS/🥈Nomal
[C/C++] 백준 12904번 - A와 B
Cocoon_
2021. 1. 30. 04:11
반응형
<코드>
#include<iostream>
#include<algorithm>
using namespace std;
string S, T;
bool ans;
string reverse(string s)
{
char tmp;
for (int i = 0; i < s.length() / 2; i++)
{
tmp = s[i];
s[i] = s[s.length() - 1 - i];
s[s.length() - 1 - i] = tmp;
}
return s;
}
void greedy(string s)
{
if (s.length() < S.length()) return;
if (S == s)
{
ans = true;
return;
}
if (s.back() == 'A')
{
s.pop_back();
greedy(s);
}
else // (s.back() == 'B')
{
s.pop_back();
greedy(reverse(s));
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> S;
cin >> T;
greedy(T);
cout << ans;
}
풀이 방법
문자열 T에서 거꾸로 연산 후 재귀함수로 보내서 S가 만들어 지는지 판별한다.
12904번: A와 B
수빈이는 A와 B로만 이루어진 영어 단어가 존재한다는 사실에 놀랐다. 대표적인 예로 AB (Abdominal의 약자), BAA (양의 울음 소리), AA (용암의 종류), ABBA (스웨덴 팝 그룹)이 있다. 이런 사실에 놀란 수
www.acmicpc.net
반응형