<p>You are given a string <code>target</code>, an array of strings <code>words</code>, and an integer array <code>costs</code>, both arrays of the same length.</p>

<p>Imagine an empty string <code>s</code>.</p>

<p>You can perform the following operation any number of times (including <strong>zero</strong>):</p>

<ul>
	<li>Choose an index <code>i</code> in the range <code>[0, words.length - 1]</code>.</li>
	<li>Append <code>words[i]</code> to <code>s</code>.</li>
	<li>The cost of operation is <code>costs[i]</code>.</li>
</ul>

<p>Return the <strong>minimum</strong> cost to make <code>s</code> equal to <code>target</code>. If it&#39;s not possible, 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">target = &quot;abcdef&quot;, words = [&quot;abdef&quot;,&quot;abc&quot;,&quot;d&quot;,&quot;def&quot;,&quot;ef&quot;], costs = [100,1,1,10,5]</span></p>

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

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

<p>The minimum cost can be achieved by performing the following operations:</p>

<ul>
	<li>Select index 1 and append <code>&quot;abc&quot;</code> to <code>s</code> at a cost of 1, resulting in <code>s = &quot;abc&quot;</code>.</li>
	<li>Select index 2 and append <code>&quot;d&quot;</code> to <code>s</code> at a cost of 1, resulting in <code>s = &quot;abcd&quot;</code>.</li>
	<li>Select index 4 and append <code>&quot;ef&quot;</code> to <code>s</code> at a cost of 5, resulting in <code>s = &quot;abcdef&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">target = &quot;aaaa&quot;, words = [&quot;z&quot;,&quot;zz&quot;,&quot;zzz&quot;], costs = [1,10,100]</span></p>

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

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

<p>It is impossible to make <code>s</code> equal to <code>target</code>, so we return -1.</p>
</div>

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

<ul>
	<li><code>1 &lt;= target.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words.length == costs.length &lt;= 5 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words[i].length &lt;= target.length</code></li>
	<li>The total sum of <code>words[i].length</code> is less than or equal to <code>5 * 10<sup>4</sup></code>.</li>
	<li><code>target</code> and <code>words[i]</code> consist only of lowercase English letters.</li>
	<li><code>1 &lt;= costs[i] &lt;= 10<sup>4</sup></code></li>
</ul>

from collections import deque

class TrieNode():
    def __init__(self):
        self.suffixLink = None
        self.output = defaultdict(lambda:10e5)
        self.children = {}
        self.id = -1

class AhoCorasick:
    def __init__(self, words, costs):
        self.initWords(words, costs)
    
    def initWords(self, words, costs):
        self.root = TrieNode()
        
        for i, word in enumerate(words):
            root = self.root
            for c in word:
                if c not in root.children:
                    root.children[c] = TrieNode()
                root = root.children[c]
            if root.id == -1 or costs[i] < costs[root.id]:
                root.id = i
            root.output[len(word)] = min(root.output[len(word)], costs[i])
        
        self.buildAutomata()
    
    def buildAutomata(self):
        q = deque([])
        
        for c, node in self.root.children.items(): 
            q.append(node)
            node.suffixLink = self.root
        
        while q:
            cur = q.popleft()

            for c, node in cur.children.items():
                ptr = cur.suffixLink
                while ptr and c not in ptr.children:
                    ptr = ptr.suffixLink
                
                if ptr and c in ptr.children:
                    node.suffixLink = ptr.children[c]
                else:
                    node.suffixLink = self.root
                
                if node.suffixLink.id >= 0:
                    node.id = node.suffixLink.id
                
                if node.suffixLink is not self.root:
                    for length, cost in node.suffixLink.output.items():
                        node.output[length] = min(node.output[length], cost)
                
                q.append(node)

    def suffixesAfterAppending(self, node, letter):
        while node != self.root and letter not in node.children:
            node = node.suffixLink
        
        if letter in node.children:
            node = node.children[letter]
        else:
            node = self.root
        
        return node
    
class Solution:
    def minimumCost(self, target: str, words: List[str], costs: List[int]) -> int:
        trie = AhoCorasick(words, costs)
        n = len(target)
        dp = [math.inf] * (n+1)
        dp[0] = 0
        cur = trie.root
    
        for i in range(1, n+1):
            cur = trie.suffixesAfterAppending(cur, target[i-1])

            for length, cost in cur.output.items():
                dp[i] = min(dp[i], dp[i-length]+cost)
           
        return -1 if dp[n] == math.inf else dp[n]
70500