File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed
weekly/week10/BOJ_12919_A와B2 Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change 1+ // A와 B 2
2+ // 브루트포스, 재귀, 가지치기
3+
4+ /**
5+ * 처음에 재귀로 브루트포스로 구현했는데 시간초과 발생. 가지치기가 가능한 문제였다.
6+ * S->T 방향이 아닌 T->S 의 방향으로 나아갔다. 역방향으로 탐색 할 때 가지치기는 다음과 같다.
7+ * 현재 탐색중인 문자열의 맨 뒤가 'A' 인가? -> 아니면 이 문자열은 A를 추가해서 만들어진 문자열이 아니다(경로 차단)
8+ * 맨 앞이 'B' 인가? -> 아니면 이 문자열은 B를 추가하고 뒤집어져서 만들어진 문자열이 아니다.
9+ * S->T 였으면 그냥 길이에 맞춰서 ABABA, BABABA 두갈래로 계속 탐색을 하겠지만,
10+ * T->S 방향에서는 T가 만들어 질 수 있는 가능성을 체크 할 수 있기 때문에 가지치기가 가능해진다.
11+ */
12+ public class BOJ_12919 {
13+
14+ static String S ;
15+ static int result = 0 ;
16+ public static void main (String [] args ) throws IOException {
17+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
18+ S = br .readLine ();
19+ String T = br .readLine ();
20+
21+ checkChange (T );
22+
23+ System .out .println (result );
24+ }
25+
26+ private static void checkChange (String t ) {
27+ if (S .length () == t .length ()){
28+ if (S .equals (t )){
29+ result = 1 ;
30+ }
31+ return ;
32+ }
33+
34+ if (t .charAt (t .length () - 1 ) == 'A' ){
35+ checkChange (t .substring (0 , t .length () - 1 ));
36+ }
37+
38+ if (t .charAt (0 ) == 'B' ){
39+ String reversed = new StringBuilder (t .substring (1 )).reverse ().toString ();
40+ checkChange (reversed );
41+ }
42+ }
43+ }
You can’t perform that action at this time.
0 commit comments