Baekjoon Review
[Gold 5] 12919 A와 B 2
hanseongbugi
2024. 3. 3. 18:34
https://www.acmicpc.net/problem/12919
12919번: A와 B 2
수빈이는 A와 B로만 이루어진 영어 단어 존재한다는 사실에 놀랐다. 대표적인 예로 AB (Abdominal의 약자), BAA (양의 울음 소리), AA (용암의 종류), ABBA (스웨덴 팝 그룹)이 있다. 이런 사실에 놀란 수빈
www.acmicpc.net
이 문제는 T를 통해 S를 만들 수 있는지 확인하는 문제이다.
입력으로 S와 T가 들어온다면, S와 T가 같은지 확인인다.
만약 다르다면 문자열의 길이가 다르다면 A와 B를 붙일 수 없으므로 제귀를 멈춘다.
만약 T의 끝이 A인 경우 temp 변수에 T를 담고 A를 제거하고 다시 문자를 붙일 준비를 한다.
만약 T의 앞이 B인 경우 temp 변수에 T를 담고 문자열을 뒤집은 후 temp의 끝을 제거한다.
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
void calcString(string s, string t){
if(s==t) {
cout<<1<<'\n';
exit(0);
}
if(s.length() > t.length()) return;
if(t[t.length() - 1] == 'A'){
string temp = t;
temp.pop_back();
calcString(s,temp);
}
if(t[0] == 'B'){
string temp = t;
reverse(temp.begin(),temp.end());
temp.pop_back();
calcString(s,temp);
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string S,T;
cin>>S;
cin>>T;
calcString(S,T);
cout<<0<<'\n';
return 0;
}