<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 itertools import zip_longest, islice


def to_int_keys_best(l):
    """
    l: iterable of keys
    returns: a list with integer keys
    """
    seen = set()
    ls = []
    for e in l:
        if not e in seen:
            ls.append(e)
            seen.add(e)
    ls.sort()
    index = {v: i for i, v in enumerate(ls)}
    return [index[v] for v in l]


def suffix_array(s):
    """
    suffix array of s
    O(n * log(n)^2)
    """
    n = len(s)
    k = 1
    line = to_int_keys_best(s)
    while max(line) < n - 1:
        line = to_int_keys_best(
            [a * (n + 1) + b + 1
             for (a, b) in
             zip_longest(line, islice(line, k, None),
                         fillvalue=-1)])
        k <<= 1
    rk = line
    sa = [0] * n
    for i in range(n):
        sa[rk[i]] = i
    return sa, rk


def suffix_array_height(s):
    n = len(s)
    sa, rk = suffix_array(s)
    height = [0] * n
    k = 0
    for i in range(n):
        if rk[i] == 0: continue
        if k: k -= 1
        j = sa[rk[i] - 1]
        while i + k < n and j + k < n and s[i + k] == s[j + k]:
            k += 1
        height[rk[i]] = k
    return sa, rk, height


def build_sparse_table(arr):
    n = len(arr)
    k = math.floor(math.log2(n)) + 1
    sparse_table = [[0] * k for _ in range(n)]

    for i in range(n):
        sparse_table[i][0] = arr[i]

    j = 1
    while (1 << j) <= n:
        i = 0
        while (i + (1 << j) - 1) < n:
            sparse_table[i][j] = min(sparse_table[i][j - 1], sparse_table[i + (1 << (j - 1))][j - 1])
            i += 1
        j += 1

    return sparse_table


def query_sparse_table(sparse_table, l, r):
    j = int(math.log2(r - l + 1))
    return min(sparse_table[l][j], sparse_table[r - (1 << j) + 1][j])


class Solution:
    def minValidStrings(self, words: List[str], target: str) -> int:
        n = len(target)
        tmp = []
        cur_idx = 0
        word_idx = []
        for word in words:
            word_idx.append(cur_idx)
            tmp.append(word)
            tmp.append('#')
            cur_idx += (len(word) + 1)
        tmp.append(target)
        target_idx = cur_idx
        s = ''.join(tmp)
        sa, rk, height = suffix_array_height(s)

        height_sparse_table = build_sparse_table(height)

        dp = [float('inf')] * (n + 1)
        dp[0] = 0
        j = 0
        for i in range(n):
            if dp[i] == float('inf'):
                return -1
            lcs = 0
            for idx1 in word_idx:
                r1, r2 = rk[idx1], rk[target_idx + i]
                if r1 > r2:
                    r1, r2 = r2, r1
                lcs = max(lcs, query_sparse_table(height_sparse_table, r1 + 1, r2))
            while j < n and j < i + lcs:
                j += 1
                dp[j] = dp[i] + 1

        return dp[-1] if dp[-1] != float('inf') else -1
    
71441