|
|
@ -56,11 +56,11 @@ public class StickersToSpellWord {
|
|
|
|
char[] s = sticker.toCharArray();
|
|
|
|
char[] s = sticker.toCharArray();
|
|
|
|
char[] t = target.toCharArray();
|
|
|
|
char[] t = target.toCharArray();
|
|
|
|
int[] count = new int[26];
|
|
|
|
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++) {
|
|
|
|
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();
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
for (int i = 0; i < count.length; i++) {
|
|
|
|
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 {
|
|
|
|
static class DpZuo {
|
|
|
|
public static int minStickers(String[] stickers, String target) {
|
|
|
|
public static int minStickers(String[] stickers, String target) {
|
|
|
|
int N = stickers.length;
|
|
|
|
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<String, Integer> 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<String, Integer> 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){
|
|
|
|
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[] 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";
|
|
|
|
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(Dp1.minStickers(stickers, target));
|
|
|
|
|
|
|
|
System.out.println(Dp2.minStickers(stickers, target));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|