https://www.acmicpc.net/problem/12919
/**
* 12919_A와 B 2
*
* T의 글자를 뒤에서부터 하나씩 제거하면서 비교한다.
* T의 맨 끝 글자가 A이면 그 글자 A를 빼준다.
* T의 맨 앞 글자가 B이면 그 글자 B를 빼주고 뒤집어준다.
*
* S와 T의 길이가 같고 S==T일 경우 1을 반환한다.
*/
public class P_12919 {
static String S, T;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
S = br.readLine();
T = br.readLine();
int result = dfs(S, T);
System.out.println(result);
}
static int dfs(String s, String t){
if(t.length() == s.length()){
if(t.equals(s)){
return 1;
}
return 0;
}
int ans = 0;
if(t.charAt(t.length() - 1) == 'A'){
ans += dfs(s, t.substring(0, t.length()-1));
}
if(t.charAt(0) == 'B'){
StringBuilder reverse = new StringBuilder();
reverse.append(t.substring(1));
String temp = reverse.reverse().toString();
ans += dfs(s, temp);
}
if(ans > 0){
return 1;
}
else {
return 0;
}
}
}