<p>Given the strings <code>s1</code> and <code>s2</code> of size <code>n</code> and the string <code>evil</code>, return <em>the number of <strong>good</strong> strings</em>.</p>

<p>A <strong>good</strong> string has size <code>n</code>, it is alphabetically greater than or equal to <code>s1</code>, it is alphabetically smaller than or equal to <code>s2</code>, and it does not contain the string <code>evil</code> as a substring. Since the answer can be a huge number, return this <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

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

<pre>
<strong>Input:</strong> n = 2, s1 = &quot;aa&quot;, s2 = &quot;da&quot;, evil = &quot;b&quot;
<strong>Output:</strong> 51 
<strong>Explanation:</strong> There are 25 good strings starting with &#39;a&#39;: &quot;aa&quot;,&quot;ac&quot;,&quot;ad&quot;,...,&quot;az&quot;. Then there are 25 good strings starting with &#39;c&#39;: &quot;ca&quot;,&quot;cc&quot;,&quot;cd&quot;,...,&quot;cz&quot; and finally there is one good string starting with &#39;d&#39;: &quot;da&quot;.&nbsp;
</pre>

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

<pre>
<strong>Input:</strong> n = 8, s1 = &quot;leetcode&quot;, s2 = &quot;leetgoes&quot;, evil = &quot;leet&quot;
<strong>Output:</strong> 0 
<strong>Explanation:</strong> All strings greater than or equal to s1 and smaller than or equal to s2 start with the prefix &quot;leet&quot;, therefore, there is not any good string.
</pre>

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

<pre>
<strong>Input:</strong> n = 2, s1 = &quot;gx&quot;, s2 = &quot;gz&quot;, evil = &quot;x&quot;
<strong>Output:</strong> 2
</pre>

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

<ul>
	<li><code>s1.length == n</code></li>
	<li><code>s2.length == n</code></li>
	<li><code>s1 &lt;= s2</code></li>
	<li><code>1 &lt;= n &lt;= 500</code></li>
	<li><code>1 &lt;= evil.length &lt;= 50</code></li>
	<li>All strings consist of lowercase English letters.</li>
</ul>

#include <vector>
#include <string>
#include <algorithm>
#include <numeric>

using namespace std;

static int speedup = []{ios::sync_with_stdio(0); cin.tie(0); return 0;}();
constexpr int mod = 1'000'000'007;

int lcs[50], ct[50][26], ctb[50][26];

void build_lcs(const string &s) {
    int N = s.size(), i = 1, l = 0;
    while (i < N) {
        if (s[i] == s[l]) lcs[i++] = ++l;
        else if (l) l = lcs[l-1];
        else lcs[i++] = 0;
    }
}

void init(const string &s) {
    build_lcs(s);
    int N = s.size();
    fill(ct[0], ct[0]+26, 0);
    ct[0][s[0]-'a'] = 1;
    for (int i = 1; i < N; ++i) {
        int p = lcs[i-1];
        copy(ct[p], ct[p]+26, ct[i]);
        ct[i][s[i]-'a'] = i+1;
    }
    for (int i = 0; i < N; ++i) {
        int w = 0;
        for (int j = 0; j < 26; ++j) if (int u = ct[i][j]; u && u != i+1) ctb[i][w++] = u;
        ctb[i][w] = 0;
    }
}

long long evalTo(const string &to, int N, bool incl) {
    int n = to.size();
    bool canAdd = true;
    long long b[N+1], res = 0;
    fill(b, b+(N+1), 0);

    for (int i = 0, l = 0; i < n; ++i) {
        long long cur = b[0];
        b[0] = 0;
        for (int j = 0; j < N; ) {
            int cnt = 0;
            for (int x; (x = ctb[j][cnt]); ++cnt) b[x] += cur;
            b[0] += (25-cnt) * cur;
            swap(cur, b[++j]);
        }

        if (canAdd) {
            char limit = to[i] - 'a';
            for (int j = 0; j < limit; ++j) ++b[ct[l][j]];
            l = ct[l][limit];
            if (l == N) canAdd = false;
        }

        for (int j = 0; j < N; ++j) b[j] %= mod;
        b[N] = 0;
    }
    return accumulate(b, b+N, 0ll) + (incl && canAdd);
}

class Solution {
public:
    int findGoodStrings(int n, const string &s1, const string &s2, const string &evil) {
        init(evil);
        int N = evil.size();
        int r = (evalTo(s2, N, true) - evalTo(s1, N, false)) % mod;
        return r >= 0 ? r : r + mod;
    }
};
7800