<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 Segment:
    def __init__(self, n):
        self.t = [float('inf')] * (4 * n + 1)
        self.lazy = [0] * (4 * n + 1)
        self.Upropagated = [0] * (4 * n + 1)

    def buildTree(self, arr, v, tl, tr):
        if tl == tr:
            self.t[v] = arr[tl]
            return
        tm = (tl + tr) // 2
        self.buildTree(arr, 2 * v, tl, tm)
        self.buildTree(arr, 2 * v + 1, tm + 1, tr)
        self.t[v] = min(self.t[2 * v], self.t[2 * v + 1])

    def apply(self, v, tl, tr, val):
        if tr != tl:
            self.lazy[v] = 1
            self.Upropagated[v] += val
        self.t[v] += (tr - tl + 1) * val

    def pushDown(self, v, tl, tr):
        if self.lazy[v] == 0:
            return
        self.lazy[v] = 0
        tm = (tl + tr) // 2
        self.apply(2 * v, tl, tm, self.Upropagated[v])
        self.apply(2 * v + 1, tm + 1, tr, self.Upropagated[v])
        self.Upropagated[v] = 0

    def update(self, v, tl, tr, pos, val):
        if tl == tr:  # When we are at the leaf node
            self.t[v] = val  # Apply the point update
            return
        tm = (tl + tr) // 2
        if pos <= tm:
            self.update(2 * v, tl, tm, pos, val)  # Update left child
        else:
            self.update(2 * v + 1, tm + 1, tr, pos, val)  # Update right child
        self.t[v] = min(self.t[2 * v], self.t[2 * v + 1])

    def query(self, v, tl, tr, l, r):
        if tl > r or tr < l:
            return float('inf')
        if l <= tl and tr <= r:
            return self.t[v]
        self.pushDown(v, tl, tr)
        tm = (tl + tr) // 2
        leftsubtree = self.query(2 * v, tl, tm, l, r)
        rightsubtree = self.query(2 * v + 1, tm + 1, tr, l, r)
        return min(leftsubtree, rightsubtree)

def zfunction(text):
    z = [0] * len(text)
    l, r = 0, 0
    
    for i in range(1, len(text)):
        if i < r:
            z[i]=min(r-i,z[i-l])
        while i + z[i] < len(text) and text[z[i]] == text[i + z[i]]:
            z[i] += 1
        if i + z[i] > r:
            l, r = i, i + z[i]
    
    return z

class Solution:
    def minValidStrings(self, words, target):
        wordCount = len(words)
        lenTarget = len(target)

        matchLen = [[0] * wordCount for _ in range(lenTarget)]

        for i in range(wordCount):
            z = zfunction(words[i] + "$" + target)
            for j in range(lenTarget):
                matchLen[j][i] = z[j + len(words[i]) + 1]

        dp = [float('inf')] * (lenTarget + 1)
        dp[lenTarget] = 0
        segment = Segment(len(dp))
        segment.buildTree(dp, 1, 0, len(dp) - 1)
        
        
        for i in range(lenTarget - 1, -1, -1):
            maxMatchLen = 0
            for j in range(wordCount):
                maxMatchLen = max(maxMatchLen, matchLen[i][j])

            firstInd = i + 1
            lastInd = i + maxMatchLen - 1
            if firstInd <= lenTarget:
                dp[i] = 1 + segment.query(1, 0, len(dp) - 1, firstInd, lastInd + 1)
                segment.update(1, 0, len(dp) - 1, i, dp[i])

        return dp[0] if dp[0] < float('inf') else -1

73118