Composing Of String (Codeforces 774I)
Tags: DP, *special

Stepan has a set of n strings. Also, he has a favorite string s. Stepan wants to do the following. He will take some strings of his set and write them down one after another. It is possible that he will take some strings more than once, and will not take some of them at all.Your task is to determine the minimum number of strings in the set which Stepan needs to take and write so that the string s appears as a subsequence in the resulting written down string. For example, in the string "abcd" strings "ad", "acd", "abcd" appear as subsequences, and strings "ba", "abdc" don't appear as subsequences. 

Input: The first line contains the integer n (1 ≤ n ≤ 50) — the number of strings in Stepan's set.The next n lines contain n non-empty strings consisting of lowercase letters of the English alphabet. The length of each of these strings does not exceed 50 symbols. It is possible that some strings from Stepan's set are the same.The next line contains the non-empty string s, consisting of lowercase letters of the English alphabet — Stepan's favorite string. The length of this string doesn't exceed 2500 symbols.

Output: Print the minimum number of strings which Stepan should take from the set and write them down one after another so that the string s appears as a subsequence in the resulting written down string. Each string from the set should be counted as many times as Stepan takes it from the set. If the answer doesn't exsist, print -1.

Note: In the first test, Stepan can take, for example, the third and the second strings from the set, write them down, and get exactly his favorite string.In the second example Stepan can take, for example, the second, the third and again the second strings from the set and write them down. Then he will get a string "aabaaaab", in which his favorite string "baaab" is a subsequence.In the third test Stepan can not get his favorite string, because it contains the letter "c", which is not presented in any of the strings in the set.

Sample Inputs:
3
a
aa
a
aaa

4
ab
aab
aa
bb
baaab

2
aaa
bbb
aaacbbb

Sample Outputs:
2

3

-1

Solution (Python):
import sys
import math
MAXN = 55
INF = 1000000000.0
MOD = 1000000000.0 + 7
L_INF = 4e+18
EPS = 1e-10
n = int(input())
s = []
for i in range(n):
    s.append(input())
t = input()
l = len(t)
dp = [INF] * (l + 1)
dp[0] = 0
for i in range(l):
    for j in range(n):
        (_l, _r) = (i, 0)
        while _r < len(s[j]) and _l < l:
            if s[j][_r] == t[_l]:
                _l += 1
            _r += 1
        dp[_l] = min(dp[_l], dp[i] + 1)
if dp[l] >= INF / 2:
    dp[l] = -1
print(dp[l])

Solution (Python):
import sys
import math
MAXN = 55
INF = 1000000000.0
MOD = 1000000000.0 + 7
L_INF = 4e+18
EPS = 1e-10
n = int(input())
s = []
for i in range(n):
    s.append(input())
t = input()
l = len(t)
dp = [INF] * (l + 1)
dp[0] = 0
for i in range(l):
    for j in range(n):
        (_l, _r) = (i, 0)
        while _r < len(s[j]) and _l < l:
            if s[j][_r] == t[_l]:
                _l += 1
            _r += 1
        dp[_l] = min(dp[_l], dp[i] + 1)
if dp[l] >= INF / 2:
    dp[l] = -1
print(dp[l])

Solution (Python):
import sys
import math
MAXN = 55
INF = 1000000000.0
MOD = 1000000000.0 + 7
L_INF = 4e+18
EPS = 1e-10
n = int(input())
s = []
for i in range(n):
    s.append(input())
t = input()
l = len(t)
dp = [INF] * (l + 1)
dp[0] = 0
for i in range(l):
    for j in range(n):
        (_l, _r) = (i, 0)
        while _r < len(s[j]) and _l < l:
            if s[j][_r] == t[_l]:
                _l += 1
            _r += 1
        dp[_l] = min(dp[_l], dp[i] + 1)
if dp[l] >= INF / 2:
    dp[l] = -1
print(dp[l])