<p>You are given a string <code>text</code>. You should split it to k substrings <code>(subtext<sub>1</sub>, subtext<sub>2</sub>, ..., subtext<sub>k</sub>)</code> such that:</p>

<ul>
	<li><code>subtext<sub>i</sub></code> is a <strong>non-empty</strong> string.</li>
	<li>The concatenation of all the substrings is equal to <code>text</code> (i.e., <code>subtext<sub>1</sub> + subtext<sub>2</sub> + ... + subtext<sub>k</sub> == text</code>).</li>
	<li><code>subtext<sub>i</sub> == subtext<sub>k - i + 1</sub></code> for all valid values of <code>i</code> (i.e., <code>1 &lt;= i &lt;= k</code>).</li>
</ul>

<p>Return the largest possible value of <code>k</code>.</p>

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

<pre>
<strong>Input:</strong> text = &quot;ghiabcdefhelloadamhelloabcdefghi&quot;
<strong>Output:</strong> 7
<strong>Explanation:</strong> We can split the string on &quot;(ghi)(abcdef)(hello)(adam)(hello)(abcdef)(ghi)&quot;.
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;merchant&quot;
<strong>Output:</strong> 1
<strong>Explanation:</strong> We can split the string on &quot;(merchant)&quot;.
</pre>

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

<pre>
<strong>Input:</strong> text = &quot;antaprezatepzapreanta&quot;
<strong>Output:</strong> 11
<strong>Explanation:</strong> We can split the string on &quot;(a)(nt)(a)(pre)(za)(tep)(za)(pre)(a)(nt)(a)&quot;.
</pre>

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

<ul>
	<li><code>1 &lt;= text.length &lt;= 1000</code></li>
	<li><code>text</code> consists only of lowercase English characters.</li>
</ul>

class Solution {
public:
    vector<vector<long long>> v;
    int dp[1001][1001];

    void help(string &text) {
        int n = text.size();
        v.resize(n, vector<long long>(n));
        int p = 31;
        long long m = 1e9+9;
        for (int i = 0; i < n; i++) {
            long long hash_value = 0;
            long long p_pow = 1;
            for (int j = i; j < n; j++) {
                hash_value = (hash_value + (text[j] - 'a' + 1) * p_pow) % m;
                p_pow = (p_pow * p) % m;
                v[i][j] = hash_value;
            }
        }
    }

    int solve(int left, int right) {
        if (left > right) return 0;
        if (dp[left][right] != -1) return dp[left][right];    
        int ans = 1;
        for (int k = 0; k < (right - left + 1) / 2; k++) {
            if (v[left][left + k] == v[right - k][right]) {
                ans = max(2 + solve(left + k + 1, right - k - 1), ans);
            }
        }
        return dp[left][right] = ans;
    }

    int longestDecomposition(string text) {
        help(text);
        memset(dp, -1, sizeof(dp));
        return solve(0, text.size() - 1);
    }
};

79805