반응형
<코드>
#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
반응형
'🧩PS > 🥈Nomal' 카테고리의 다른 글
[C/C++] 백준 7576번 - 토마토 (BFS) (0) | 2021.01.31 |
---|---|
[C/C++] 백준 1322번 - X와 K (0) | 2021.01.30 |
[C/C++] 백준 17626번 - Four Squares (3) | 2021.01.30 |
[C/C++] 백준 3109번 - 빵집 (0) | 2021.01.28 |
[C/C++] 백준 3673번 - 나눌 수 있는 부분 수열 (0) | 2021.01.28 |