diff --git "a/0224LJH/202511/12 BOJ \354\225\214 \354\210\230 \354\227\206\353\212\224 \353\254\270\354\236\245.md" "b/0224LJH/202511/12 BOJ \354\225\214 \354\210\230 \354\227\206\353\212\224 \353\254\270\354\236\245.md" new file mode 100644 index 00000000..992c3f2b --- /dev/null +++ "b/0224LJH/202511/12 BOJ \354\225\214 \354\210\230 \354\227\206\353\212\224 \353\254\270\354\236\245.md" @@ -0,0 +1,106 @@ +```java +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.*; +import java.util.*; + +public class Main { + + + static char[] arr; + static int[] dp; + static int cnt,ans,len; + + static final int CHAR_LEN = 'z'-'a'+1; + static final int MAX = Integer.MAX_VALUE/2; + static final int X = -1; + + static HashSet words = new HashSet<>(); + + static class Word{ + //각 알파벳 몇번 사용했는지 카운트 + int[] count = new int[CHAR_LEN]; + char[] content; + + public Word(String w) { + content = w.toCharArray(); + for (int i = 0; i < content.length; i++) { + count[content[i]-'a']++; + } + } + + public int getCost(int idx) { + int end = idx+ content.length; + if (arr.length < end) { + return X; + } + + int[] targetCnt = new int[CHAR_LEN]; + for (int i = idx; i < end; i++) { + targetCnt[arr[i]-'a']++; + } + + for (int i = 0; i< CHAR_LEN; i++) { + if (targetCnt[i] != count[i]) return X; + } + + int cost = 0; + + for (int i = 0; i < content.length; i++) { + if (arr[i+idx] != content[i])cost++; + } + + + + return cost; + } + + } + + + public static void main (String[] args) throws NumberFormatException, IOException { + init(); + + process(); + print(); + + } + + public static void init() throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + arr = br.readLine().toCharArray(); + cnt = Integer.parseInt(br.readLine()); + ans = MAX; + len = arr.length; + dp = new int[len+1]; + + + Arrays.fill(dp,MAX); + dp[0] = 0; + + for (int i = 0; i < cnt; i++) { + String word = br.readLine(); + words.add(new Word(word)); + } + + } + + public static void process() { + for (int i = 0; i < len; i++) { + for (Word w: words) { + int cost = w.getCost(i); + if (cost == X) continue; + + dp[i+w.content.length] = Math.min(dp[i+w.content.length], dp[i]+cost); + } + } + } + + public static void print() { + System.out.println(dp[len]==MAX?-1:dp[len]); + } + +} + +```