<p>You are <strong>building</strong> a string <code>s</code> of length <code>n</code> <strong>one</strong> character at a time, <strong>prepending</strong> each new character to the <strong>front</strong> of the string. The strings are labeled from <code>1</code> to <code>n</code>, where the string with length <code>i</code> is labeled <code>s<sub>i</sub></code>.</p>

<ul>
	<li>For example, for <code>s = &quot;abaca&quot;</code>, <code>s<sub>1</sub> == &quot;a&quot;</code>, <code>s<sub>2</sub> == &quot;ca&quot;</code>, <code>s<sub>3</sub> == &quot;aca&quot;</code>, etc.</li>
</ul>

<p>The <strong>score</strong> of <code>s<sub>i</sub></code> is the length of the <strong>longest common prefix</strong> between <code>s<sub>i</sub></code> and <code>s<sub>n</sub></code> (Note that <code>s == s<sub>n</sub></code>).</p>

<p>Given the final string <code>s</code>, return<em> the <strong>sum</strong> of the <strong>score</strong> of every </em><code>s<sub>i</sub></code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;babab&quot;
<strong>Output:</strong> 9
<strong>Explanation:</strong>
For s<sub>1</sub> == &quot;b&quot;, the longest common prefix is &quot;b&quot; which has a score of 1.
For s<sub>2</sub> == &quot;ab&quot;, there is no common prefix so the score is 0.
For s<sub>3</sub> == &quot;bab&quot;, the longest common prefix is &quot;bab&quot; which has a score of 3.
For s<sub>4</sub> == &quot;abab&quot;, there is no common prefix so the score is 0.
For s<sub>5</sub> == &quot;babab&quot;, the longest common prefix is &quot;babab&quot; which has a score of 5.
The sum of the scores is 1 + 0 + 3 + 0 + 5 = 9, so we return 9.</pre>

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

<pre>
<strong>Input:</strong> s = &quot;azbazbzaz&quot;
<strong>Output:</strong> 14
<strong>Explanation:</strong> 
For s<sub>2</sub> == &quot;az&quot;, the longest common prefix is &quot;az&quot; which has a score of 2.
For s<sub>6</sub> == &quot;azbzaz&quot;, the longest common prefix is &quot;azb&quot; which has a score of 3.
For s<sub>9</sub> == &quot;azbazbzaz&quot;, the longest common prefix is &quot;azbazbzaz&quot; which has a score of 9.
For all other s<sub>i</sub>, the score is 0.
The sum of the scores is 2 + 3 + 9 = 14, so we return 14.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

using ll=int64_t;
ll quickHash(string s, ll p=31, ll mod=999999929) {
    ll ans=s[0]-'a'+1;
    for (int i=1;i<s.length();i++)
        ans=(ans*p+(s[i]-'a'+1))%mod;
    return ans;
}
struct hasher {
    int sz; ll mod,p;
    vector<ll> fHash;
    vector<ll> pk; //p^( )
    void init(string &s, ll _p=31, ll _mod=999999929) {
        mod=_mod; p=_p;
        sz=s.length();
        fHash.resize(sz); pk.resize(sz);
        fHash[0]=s[0]-'a'+1;
        pk[0]=1;
        for (int i=1;i<s.length();i++) {
            fHash[i]=(fHash[i-1]*p+(s[i]-'a'+1))%mod; 
            pk[i]=pk[i-1]*p%mod;
        }
    }

    ll getfHash(int l, int r) { //s[l...r] = f[r]-f[l-1]*(p^(r-l+1))
        if (l==0) return fHash[r];
        return ((fHash[r]-fHash[l-1]*pk[r-l+1]%mod)%mod+mod)%mod;
    }
};
struct dhasher {
    hasher h1,h2;
    void init(string &s, ll p1=31, ll p2=37, ll mod1=999999929, ll mod2=999999937) {
        h1.init(s,p1,mod1);
        h2.init(s,p2,mod2);
    }
    pair<ll,ll> getfHash(int l, int r) {
        return {h1.getfHash(l,r),h2.getfHash(l,r)};
    }
};

class Solution {
public:
long long sumScores(string s) {
    dhasher h; h.init(s);
    int i=0;
    ll tot=0;
    for (int j=0;j<s.length();j++) {
        int lo=1, hi=s.length()-j, ans=0;
        while (hi>=lo) {
            ll X=(lo+hi)/2;
            if (h.getfHash(i,X+i-1)==h.getfHash(j,X+j-1)) {
                ans=X;
                lo=X+1;
            } else hi=X-1;
        }
        tot+=ans;
    }
    return tot;
}
};
68700