Algorithm/Baekjoon

[Java] ๋ฐฑ์ค€ 16500๋ฒˆ - ๋ฌธ์ž์—ด ํŒ๋ณ„

๊ฐœ๋ฐœ๊ฐœ๊ตด๐Ÿธ 2022. 7. 15. 18:38

[๋ฌธ์ œ]

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

 

16500๋ฒˆ: ๋ฌธ์ž์—ด ํŒ๋ณ„

์ฒซ์งธ ์ค„์— ๊ธธ์ด๊ฐ€ 100์ดํ•˜์ธ ๋ฌธ์ž์—ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” A์— ํฌํ•จ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ์…‹์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์—๋Š” A์— ํฌํ•จ๋œ ๋‹จ์–ด๊ฐ€ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ์ฃผ์–ด์ง„๋‹ค. A์—

www.acmicpc.net

[ํ’€์ด]

 

์•„๋ž˜์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค. 

https://code-lab1.tistory.com/222

 

[๋ฐฑ์ค€] 16500๋ฒˆ ๋ฌธ์ž์—ด ํŒ๋ณ„ (์ž๋ฐ” ํ’€์ด)

๋ฌธ์ œ https://www.acmicpc.net/problem/16500 16500๋ฒˆ: ๋ฌธ์ž์—ด ํŒ๋ณ„ ์ฒซ์งธ ์ค„์— ๊ธธ์ด๊ฐ€ 100์ดํ•˜์ธ ๋ฌธ์ž์—ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” A์— ํฌํ•จ๋œ ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค. ์…‹์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„..

code-lab1.tistory.com

 

DP๋กœ ํ’€์ดํ•ด์•ผ ํ•œ๋‹ค๋Š”๊ฑธ ์ƒ๊ฐํ•ด๋‚ด์ง€ ๋ชปํ•˜๊ณ  ๊ฒฐ๊ตญ ํ’€์ด๋ฅผ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.ใ… ใ… 

 

DP๋กœ ๋งˆ์ง€๋ง‰ index๋ถ€ํ„ฐ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ substring์„ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น substring์ด A์˜ ์ง‘ํ•ฉ set์— ํฌํ•จ๋˜์–ด์žˆ๋‹ค๋ฉด

dp[index] = 1๋กœ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ index + 1์˜ ์œ„์น˜๋ถ€ํ„ฐ dp ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๋ฉฐ 1์ด ์žˆ๋‹ค๋ฉด substring(index, 1์ด ์žˆ๋Š” index)๊ฐ€ set์— ์กด์žฌํ•œ๋‹ค๋ฉด dp[index] = 1๋กœ ๊ฐฑ์‹ ํ•ด์ค๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, s = "abcefg", set = { abc, efg, abce }์ด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค๋ฉด ํƒ์ƒ‰ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • f -> g๊ฐ€ 1์ธ์ง€ ํ™•์ธ X
  • e -> fg ์ค‘์— 1์ด ์žˆ๋Š”์ง€ ํ™•์ธ -> X (efg ๊ฐ€ set์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ e์˜ index์ธ 3์— 1์„ ์ €์žฅ : dp[3])
  • c -> efg ์ค‘ e์— 1์ด ์žˆ์œผ๋ฏ€๋กœ, c์˜ index์ธ 2๋ถ€ํ„ฐ e์˜ index์ธ 3๊นŒ์ง€์˜ substring์ธ c๊ฐ€ set์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธ -> X

์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ๋ฐ˜๋ณต ํ•˜๋ฉฐ a์— ๋„๋‹ฌํ–ˆ์„๋•Œ, bcefg์ค‘์— 1์ด ์žˆ๋Š” e๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์žก๊ณ  ํ™•์ธํ•ด๋ณธ๋‹ค๋ฉด substring(0,3)์€ abc์ด๊ณ , abc๋Š” set์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค ๋”ฐ๋ผ์„œ dp[0] = 1์ด ๋˜๊ณ  ๋ฐ˜๋ณต๋ฌธ์ด ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค.

 

dp[0] = 1 ์ด๋ผ๋Š”๊ฒƒ์€ ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค๋Š”๊ฒƒ์„ ์˜๋ฏธํ•˜๋ฏ€๋กœ 1์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

[์ฝ”๋“œ]

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        int N = Integer.parseInt(br.readLine());

        // A ๋ฌธ์ž์—ด์„ ์ž…๋ ฅ ๋ฐ›์Œ
        Set<String> set = new HashSet<>();
        for(int i=0;i<N;i++) {
            String word = br.readLine();
            if(s.contains(word)) {
                set.add(word);
            }
        }

        int[] dp = new int[s.length()];
        // ๋งˆ์ง€๋ง‰ index๋ถ€ํ„ฐ ํƒ์ƒ‰
        for(int i=s.length() -1 ;i>=0;i--) {
            for(int j=i+1;j<s.length();j++) {
                if(dp[j] == 1 && set.contains(s.substring(i,j))) {
                    dp[i] = 1;
                }
            }
            if(set.contains(s.substring(i))) {
                dp[i] = 1;
            }
        }
        System.out.println(dp[0]);
    }
}