olrlobt

[์ž๋ฐ”/๋ฐฑ์ค€ ๊ณจ๋“œ5] #12904 A์™€ B ๋ณธ๋ฌธ

Algorithm/๋ฐฑ์ค€

[์ž๋ฐ”/๋ฐฑ์ค€ ๊ณจ๋“œ5] #12904 A์™€ B

olrlobt 2022. 12. 31. 01:21

https://www.acmicpc.net/problem/12904

๐Ÿ”’ ๊ณจ๋“œ5 - #12904 A์™€ B

 

 

๐Ÿ“Œ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์ถ”๊ฐ€ ํžŒํŠธ

B
ABABABAB

= 0
A
A

= 1
ABB
AB

= 0

 

 

โœ๏ธ ํ’€์ด๋ฒ•

๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„, ๋‘ ๊ฐ€์ง€ ๊ทœ์น™์œผ๋กœ ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด๋กœ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋ฏ€๋กœ,

๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐ˜๋Œ€๋กœ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์ ์€ ์˜ˆ์™ธ๋ฅผ ์ฐพ๋Š” ๋ถ€๋ถ„์ด์—ˆ๋‹ค.

์ฒ˜์Œ๋ถ€ํ„ฐ ์„ฑ๊ณตํ•˜๋Š” ๊ฐ’์ด ๋“ค์–ด์˜ค๊ฑฐ๋‚˜, ํ•œ ์ž๋ฆฟ์ˆ˜ ๊ฐ’์ด ๋“ค์–ด์˜ค๊ฑฐ๋‚˜, ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์ด ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜, ๋“ฑ๋“ฑ ์˜ˆ์™ธ๋ฅผ ์ž˜ ์ƒ๊ฐํ•ด์„œ ์ฒ˜๋ฆฌํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ, ํ’€์ด๋ฒ•์€

    1. ๋‘ ๋ฒˆ์งธ ๋ฌธ์ž์—ด์„ ๋’ค์—์„œ๋ถ€ํ„ฐ A๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€ A๋ฅผ ์ง€์šด๋‹ค.

    2. B๋ฅผ ์ง€์šฐ๊ณ , ๋ฌธ์ž์—ด์„ ๋’ค์ง‘๋Š”๋‹ค.

    3. ์œ„์˜ ๊ณผ์ •์„ ๋ฐ˜๋ณตํ•˜์—ฌ, ์ฒซ ๋ฒˆ์งธ ๋ฌธ์ž์—ด๋ณด๋‹ค ๊ฐ™์•„์ง€๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

 

 

 

๐Ÿ—๏ธ ํ’€์ด

import java.util.Scanner;

public class baekjoon12904 {

    static String S;
    static StringBuffer T;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        S = sc.nextLine();
        String Ts = sc.nextLine();
        T = new StringBuffer(Ts);

        if(solve()){
            System.out.println("1");
        }else {
            System.out.println("0");
        }
    }

    public static boolean solve() {

        do{

            while(removeA());
        }while (T.length() > S.length() && removeB());

        return verify();
    }

    private static boolean verify() {
        if(T.toString().equals(S) ){
            return true;
        }
        return false;
    }

    private static boolean removeB() {
        if(T.charAt(T.length()-1) == 'B'){
            T.deleteCharAt(T.length()-1);
            T.reverse();
            return true;
        }
        return false;
    }

    private static boolean removeA() {

        if(T.charAt(T.length()-1 )== 'A'){
            if(T.length() <= S.length()){
                return false;
            }
            T.deleteCharAt(T.length()-1);
            return true;
        }
        return false;
    }
}
Comments