From cb5440a2a715f5a53a24795e870c0df8509f5d0a Mon Sep 17 00:00:00 2001 From: Leo <582717189@qq.com> Date: Mon, 11 Jan 2021 10:04:01 +0800 Subject: [PATCH] minStickers --- src/leo/class19/StickersToSpellWord.java | 116 ++++++++++++++++++++++- 1 file changed, 111 insertions(+), 5 deletions(-) diff --git a/src/leo/class19/StickersToSpellWord.java b/src/leo/class19/StickersToSpellWord.java index af6ca04..a2e6aaa 100644 --- a/src/leo/class19/StickersToSpellWord.java +++ b/src/leo/class19/StickersToSpellWord.java @@ -56,11 +56,11 @@ public class StickersToSpellWord { char[] s = sticker.toCharArray(); char[] t = target.toCharArray(); int[] count = new int[26]; - for (int i = 0; i < s.length; i++) { - count[s[i] - 'a']++; - } for (int i = 0; i < t.length; i++) { - count[t[i] - 'a']--; + count[t[i] - 'a']++; + } + for (int i = 0; i < s.length; i++) { + count[s[i] - 'a']--; } StringBuffer sb = new StringBuffer(); for (int i = 0; i < count.length; i++) { @@ -74,6 +74,54 @@ public class StickersToSpellWord { } } + static class Recursion1 { + public static int minStickers(String[] stickers, String target) { + if (target == null || target.length() == 0 || stickers == null || stickers.length == 0) { + return -1; + } + int ans = process(stickers, target); + return ans == Integer.MAX_VALUE ? -1 : ans; + } + + private static int process(String[] stickers, String target) { + if (target.length() == 0) { + return 0; + } + int min = Integer.MAX_VALUE; + for (String sticker : stickers) { + String rest = minus(sticker, target); + if (rest.length() != target.length()) { + min = Math.min(min, process(stickers, rest)); + } + } + return min + (min == Integer.MAX_VALUE ? 0 : 1); + } + + private static String minus(String sticker, String target) { + int[] count = new int[26]; + char[] sChar = sticker.toCharArray(); + char[] tChar = target.toCharArray(); + for (int i = 0; i < tChar.length; i++) { + count[tChar[i] - 'a']++; + } + for (int i = 0; i < sChar.length; i++) { + count[sChar[i] - 'a']--; + } + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < 26; i++) { + if (count[i] > 0) { + for (int j = 0; j < count[i]; j++) { + sb.append((char) (i + 'a')); + } + } + } + return sb.toString(); + } + + } + + + static class DpZuo { public static int minStickers(String[] stickers, String target) { int N = stickers.length; @@ -235,12 +283,70 @@ public class StickersToSpellWord { } } + static class Dp2 { + public static int minStickers(String[] stickers, String target) { + if (target == null || target.length() == 0) { + return -1; + } + int n = stickers.length; + int[][] sCount = new int[n][26]; + for (int i = 0; i < n; i++) { + char[] chars = stickers[i].toCharArray(); + for (char c : chars) { + sCount[i][c - 'a']++; + } + } + Map dp = new HashMap<>(); + dp.put("", 0); + int ans = process(sCount, target, dp); + return ans == Integer.MAX_VALUE ? -1 : ans; + } + + private static int process(int[][] stickers, String target, Map dp) { + if (dp.containsKey(target)) { + return dp.get(target); + } + if (target.length() == 0) { + return 0; + } + int[] tCount = new int[26]; + char[] t = target.toCharArray(); + for (char c : t) { + tCount[c - 'a']++; + } + int min = Integer.MAX_VALUE; + for (int i = 0; i < stickers.length; i++) { + int[] sticker = stickers[i]; + //从target第一个字符开始,剪枝 + if (sticker[t[0] - 'a'] > 0) { + StringBuffer sb = new StringBuffer(); + for (int j = 0; j < 26; j++) { + int mun = tCount[j] - sticker[j]; + for (int k = 0; k < mun; k++) { + sb.append((char) (j + 'a')); + } + } + String rest = sb.toString(); + min = Math.min(min, process(stickers, rest, dp)); + } + } + min += (min == Integer.MAX_VALUE ? 0 : 1); + dp.put(target, min); + return min; + } + + } + public static void main(String[] args){ String[] stickers = {"heavy", "claim", "seven", "set", "had", "it", "dead", "jump", "design", "question", "sugar", "dress", "any", "special", "ground", "huge", "use", "busy", "prove", "there", "lone", "window", "trip", "also", "hot", "choose", "tie", "several", "be", "that", "corn", "after", "excite", "insect", "cat", "cook", "glad", "like", "wont", "gray", "especially", "level", "when", "cover", "ocean", "try", "clean", "property", "root", "wing"}; String target = "travelbell"; - System.out.println(Dp.minStickers(stickers, target)); + /*String[] stickers = {"heavy", "claim", "seven", "set", "had", "it", "dead", "sugar"}; + String target = "travelell";*/ + /*System.out.println(Recursion1.minStickers(stickers, target)); + System.out.println(Recursion.minStickers(stickers, target));*/ System.out.println(Dp1.minStickers(stickers, target)); + System.out.println(Dp2.minStickers(stickers, target)); } }