using
System;
using
System.Collections.Generic;
public
class
Program {
static
List<List<
int
> > countMap
=
new
List<List<
int
> >();
static
int
cnt =
int
.MaxValue;
static
void
Count(
int
curCnt,
int
pos,
List<
int
> charAvailable,
string
target, List<
string
> stickers)
{
if
(curCnt >= cnt)
return
;
int
m = stickers.Count;
int
n = target.Length;
if
(pos == n) {
cnt = Math.Min(cnt, curCnt);
return
;
}
char
c = target[pos];
if
(charAvailable > 0) {
charAvailable--;
Count(curCnt, pos + 1, charAvailable, target,
stickers);
charAvailable++;
}
else
{
for
(
int
i = 0; i < m; i++) {
if
(countMap[i] == 0)
continue
;
for
(
int
j = 0; j < 26; j++) {
charAvailable[j] += countMap[i][j];
}
Count(curCnt + 1, pos, charAvailable,
target, stickers);
for
(
int
j = 0; j < 26; j++) {
charAvailable[j] -= countMap[i][j];
}
}
}
}
static
int
MinStickers(List<
string
> stickers,
string
target)
{
if
(target ==
""
)
return
-1;
if
(target.Length == 0)
return
0;
if
(stickers.Count == 0)
return
-1;
int
m = stickers.Count;
countMap =
new
List<List<
int
> >(m);
for
(
int
i = 0; i < stickers.Count; i++) {
string
s = stickers[i];
countMap.Add(
new
List<
int
>(26));
for
(
int
j = 0; j < 26; j++) {
countMap[i].Add(0);
}
foreach
(
char
c
in
s) { countMap[i]++; }
}
List<
int
> temp =
new
List<
int
>(26);
for
(
int
i = 0; i < 26; i++) {
temp.Add(0);
}
Count(0, 0, temp, target, stickers);
return
cnt ==
int
.MaxValue ? -1 : cnt;
}
public
static
void
Main()
{
List<
string
> str =
new
List<
string
>() {
"with"
,
"example"
,
"science"
};
string
target =
"thehat"
;
int
Result = MinStickers(str, target);
Console.WriteLine(Result);
}
}