<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>

from typing import List
import sys
import functools


class SegmentTree:
    def __init__(self, arr):
        self.maxInt = float("inf")
        self.st = [self.maxInt] * 4 * len(arr)
        for i, v in enumerate(arr):
            self.update(0, 0, len(arr) - 1, i, v)

    def update(self, id, left, right, i, val):
        if i < left or i > right:
            return
        if left == right:
            self.st[id] = val
            return
        mid = (left + right) // 2
        self.update(id * 2 + 1, left, mid, i, val)
        self.update(id * 2 + 2, mid + 1, right, i, val)
        self.st[id] = min(self.st[id * 2 + 1], self.st[id * 2 + 2])

    def getMin(self, id, left, right, u, v):
        # [left - right - u - v], [u - v - left - right]
        if u > right or v < left:
            return self.maxInt
        # [u - left - right - v]
        if u <= left and right <= v:
            return self.st[id]
        mid = (left + right) // 2
        minLeft = self.getMin(id * 2 + 1, left, mid, u, v)
        minRight = self.getMin(id * 2 + 2, mid + 1, right, u, v)
        return min(minLeft, minRight)


class Solution:
    def minValidStrings(self, words: List[str], target: str) -> int:
        def ch2int(ch):
            return ord(ch) - 96

        n = len(target)
        mod = 10 ** 9 + 7
        INF = float("inf")
        m = max(len(w) for w in words)
        d = [set() for i in range(m)]
        val = [0] * n
        for i in range(m):
            for j, w in enumerate(words):
                if i < len(w):
                    val[j] = (val[j] * 27 + ch2int(w[i])) % mod
                    d[i].add(val[j])
        pow_mod = [1] * (n + 1)
        for i in range(1, n + 1):
            pow_mod[i] = (pow_mod[i - 1] * 27) % mod
        pre_hash = [0] * (n + 1)
        v = 0
        for i in range(n):
            v = ((v * 27) + ch2int(target[i])) % mod
            pre_hash[i + 1] = v

        def calculate_hash(l, r):
            length = r - l + 1
            return (pre_hash[r] - pre_hash[l - 1] * pow_mod[length]) % mod

        dp = [INF] * (n + 1)
        dp[n] = 0
        st = SegmentTree(dp)
        for i in range(n - 1, -1, -1):
            v = 0
            l, r = i - 1, min(n, i + m) - 1
            while l < r:
                j = (l + r + 1) // 2
                h = calculate_hash(i + 1, j + 1)
                if h in d[j - i]:
                    l = j
                else:
                    r = j - 1
            # get min from i+1 to r+1
            dp[i] = 1 + st.getMin(0, 0, n, i + 1, r + 1)
            st.update(0, 0, n, i, dp[i])
        return dp[0] if dp[0] < INF else -1
37891