<p>You are given a string <code>s</code>. You can convert <code>s</code> to a <span data-keyword="palindrome-string">palindrome</span> by adding characters in front of it.</p>

<p>Return <em>the shortest palindrome you can find by performing this transformation</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> s = "aacecaaa"
<strong>Output:</strong> "aaacecaaa"
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> s = "abcd"
<strong>Output:</strong> "dcbabcd"
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

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

#define B 131
#define P 1000000007

typedef long long ll;

ll back_hash(string&s,vector<ll>&bhash,vector<ll>&p131,int idx){
    int N=s.size()-1;
    ll tmp=(bhash[1]-bhash[idx+1]*p131[idx]%P)%P;
    while(tmp<0)tmp+=P;
    return tmp;
}


class Solution {
public:
    string shortestPalindrome(string s) {
        if(s=="")return s;
        int N=s.size();
        s=" "+s;
        vector<ll>fhash(N+1),bhash(N+2),p131(N+1);
        p131[0]=1;
        for(int i=1;i<=N;i++)fhash[i]=(fhash[i-1]*B%P+(s[i]-'a'))%P,p131[i]=p131[i-1]*B%P;
        for(int i=N;i;i--)bhash[i]=(bhash[i+1]*B%P+(s[i]-'a'))%P;
        int idx=1;
        for(int i=1;i<=N;i++){
            if(fhash[i]==back_hash(s,bhash,p131,i))idx=i;
        }
        string tmp=s.substr(idx+1);
        reverse(tmp.begin(),tmp.end());
        return tmp+s.substr(1);
    }
};
13800