<p>You are given an array of strings <code>words</code> and a string <code>target</code>.</p>

<p>A string <code>x</code> is called <strong>valid</strong> if <code>x</code> is a <span data-keyword="string-prefix">prefix</span> of <strong>any</strong> string in <code>words</code>.</p>

<p>Return the <strong>minimum</strong> number of <strong>valid</strong> strings that can be <em>concatenated</em> to form <code>target</code>. If it is <strong>not</strong> possible to form <code>target</code>, return <code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">words = [&quot;abc&quot;,&quot;aaaaa&quot;,&quot;bcdef&quot;], target = &quot;aabcdabc&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">3</span></p>

<p><strong>Explanation:</strong></p>

<p>The target string can be formed by concatenating:</p>

<ul>
	<li>Prefix of length 2 of <code>words[1]</code>, i.e. <code>&quot;aa&quot;</code>.</li>
	<li>Prefix of length 3 of <code>words[2]</code>, i.e. <code>&quot;bcd&quot;</code>.</li>
	<li>Prefix of length 3 of <code>words[0]</code>, i.e. <code>&quot;abc&quot;</code>.</li>
</ul>
</div>

<p><strong class="example">Example 2:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">words = [&quot;abababab&quot;,&quot;ab&quot;], target = &quot;ababaababa&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">2</span></p>

<p><strong>Explanation:</strong></p>

<p>The target string can be formed by concatenating:</p>

<ul>
	<li>Prefix of length 5 of <code>words[0]</code>, i.e. <code>&quot;ababa&quot;</code>.</li>
	<li>Prefix of length 5 of <code>words[0]</code>, i.e. <code>&quot;ababa&quot;</code>.</li>
</ul>
</div>

<p><strong class="example">Example 3:</strong></p>

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">words = [&quot;abcdef&quot;], target = &quot;xyz&quot;</span></p>

<p><strong>Output:</strong> <span class="example-io">-1</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= words.length &lt;= 100</code></li>
	<li><code>1 &lt;= words[i].length &lt;= 5 * 10<sup>4</sup></code></li>
	<li>The input is generated such that <code>sum(words[i].length) &lt;= 10<sup>5</sup></code>.</li>
	<li><code>words[i]</code> consists only of lowercase English letters.</li>
	<li><code>1 &lt;= target.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>target</code> consists only of lowercase English letters.</li>
</ul>

class RabinKarp:
    mod = 344555666677777
    base = randint(mod//10, mod*9//10)
    pows = [1]
    def __init__(self, s=[]):
        self.s = []
        self.pre = [0]
        self.l = self.r = 0
        for c in s: self.append(c)
    def append(self, c):
        assert len(self.s) == self.r
        self.r += 1
        self.s.append(c)
        self.pre.append((self.pre[-1]*RabinKarp.base + hash(c)+2**63) % RabinKarp.mod)
    def pop(self):
        assert len(self.s) == self.r
        self.r -= 1
        self.s.pop()
        self.pre.pop()
    def query(self, l, r):
        l += self.l
        r += self.l
        pows = RabinKarp.pows
        while r-l >= len(pows):
            pows.append(pows[-1] * RabinKarp.base % RabinKarp.mod)
        return (self.pre[r] - self.pre[l]*pows[r-l]) % RabinKarp.mod
    def __hash__(self):
        return self.query(0, len(self))
    def __getitem__(self, w):
        if isinstance(w, int): return self.s[self.l+w]
        assert isinstance(w, slice)
        rk = RabinKarp()
        rk.s = self.s
        rk.pre = self.pre
        rk.l, rk.r, _ = w.indices(len(self))
        return rk
    def __repr__(a): return f'RabinKarp({a.s[a.l:a.r]})'
    def __eq__(a, b): return hash(a) == hash(b)
    def __len__(a): return a.r-a.l
    def lcp(a, b):
        l, r = 0, min(len(a), len(b))
        while l < r:
            h = (l+r+1)//2
            if a.query(0, h) == b.query(0, h): l = h
            else: r = h-1
        return l
    def __lt__(a, b):
        lcp = a.lcp(b)
        if lcp == min(len(a), len(b)): return len(a) < len(b)
        return a[lcp] < b[lcp]
    def __le__(a, b): return a == b or a < b

class Tree:
    def __init__(self, n, f=add, z=0):
        self.t = [z for _ in range(2*n)]
        self.n, self.f, self.z = n, f, z
    def update(self, i, v):
        i += self.n
        self.t[i] = v
        while i > 1:
            i //= 2
            self.t[i] = self.f(self.t[i*2], self.t[i*2+1])
    def query(self, l, r):
        L, R = self.z, self.z
        l += self.n
        r += self.n
        while l < r:
            if l&1:
                L = self.f(L, self.t[l])
                l += 1
            if r&1:
                r -= 1
                R = self.f(self.t[r], R)
            l //= 2
            r //= 2
        return self.f(L, R)

class Solution:
    def minValidStrings(self, words: List[str], s: str) -> int:
        ar = sorted(map(RabinKarp, words))
        rk = RabinKarp(s)
        def match(rk):
            l = bisect_left(ar, rk)
            lcp = 0
            if l < m: lcp = rk.lcp(ar[l])
            if l: lcp = max(lcp, rk.lcp(ar[l-1]))
            return lcp
        m, n = len(ar), len(s)
        dp = Tree(n+1, min, inf)
        dp.update(n, 0)
        for i in range(n)[::-1]:
            dp.update(i, dp.query(i, i+match(rk[i:])+1) + 1)
        res = dp.query(0, 1)
        return res if res < inf else -1
29503