<p>Given two strings <code>a</code> and <code>b</code>, return <em>the minimum number of times you should repeat string </em><code>a</code><em> so that string</em> <code>b</code> <em>is a substring of it</em>. If it is impossible for <code>b</code>​​​​​​ to be a substring of <code>a</code> after repeating it, return <code>-1</code>.</p>

<p><strong>Notice:</strong> string <code>&quot;abc&quot;</code> repeated 0 times is <code>&quot;&quot;</code>, repeated 1 time is <code>&quot;abc&quot;</code> and repeated 2 times is <code>&quot;abcabc&quot;</code>.</p>

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

<pre>
<strong>Input:</strong> a = &quot;abcd&quot;, b = &quot;cdabcdab&quot;
<strong>Output:</strong> 3
<strong>Explanation:</strong> We return 3 because by repeating a three times &quot;ab<strong>cdabcdab</strong>cd&quot;, b is a substring of it.
</pre>

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

<pre>
<strong>Input:</strong> a = &quot;a&quot;, b = &quot;aa&quot;
<strong>Output:</strong> 2
</pre>

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

<ul>
	<li><code>1 &lt;= a.length, b.length &lt;= 10<sup>4</sup></code></li>
	<li><code>a</code> and <code>b</code> consist of lowercase English letters.</li>
</ul>

int mod0=1000000007,mod1=1000000009;
int inv0=190839696,inv1=262773725;
int base0=131,base1=137;
vector<long long> hashValue(int n,string s){
    long long power0=base0;
    long long power1=base1;
    vector<long long> ans(2,s[0]);
    for(int i=1;i<n;i++){
        ans[0]=(ans[0]+power0*s[i])%mod0;
        ans[1]=(ans[1]+power1*s[i])%mod1;
        power0=(power0*base0)%mod0;
        power1=(power1*base1)%mod1;
    }
    return ans;
}
vector<vector<long long>> hashPrefix(int n,string s){
    long long power0=base0;
    long long power1=base1;
    vector<vector<long long>> ans(n,vector<long long>(2,s[0]));
    for(int i=1;i<n;i++){
        ans[i][0]=(ans[i-1][0]+power0*s[i])%mod0;
        ans[i][1]=(ans[i-1][1]+power1*s[i])%mod1;
        power0=(power0*base0)%mod0;
        power1=(power1*base1)%mod1;
    }
    return ans;
}
vector<vector<long long>> invPower(int n){
    vector<vector<long long>> ans(n,vector<long long>(2,1));
    for(int i=1;i<n;i++){
        ans[i][0]=(ans[i-1][0]*inv0)%mod0;
        ans[i][1]=(ans[i-1][1]*inv1)%mod1;
    }
    return ans;
}
vector<long long> subarray(vector<vector<long long>> &hash,vector<vector<long long>> &inve,int start,int end){
    vector<long long> value(2);
    value[0]=((hash[end][0]-(start?hash[start-1][0]:0)+mod0)*inve[start][0])%mod0;
    value[1]=((hash[end][1]-(start?hash[start-1][1]:0)+mod1)*inve[start][1])%mod1;
    return value;
}
bool solve(string s,string t){
    int n=s.size(),m=t.size();
    vector<vector<long long>> hash=hashPrefix(n,s);
    vector<vector<long long>> inve=invPower(n);
    vector<long long> patt=hashValue(m,t);
    for(int i=0;i+m-1<n;i++){
        if(subarray(hash,inve,i,i+m-1)==patt) return true;
    }
    return false;
}
class Solution {
public:
    int repeatedStringMatch(string a, string b) {
        string res;
        int cnt=0;
        while(res.size()<b.size()){
            res+=a;
            cnt+=1;
        }
        if(solve(res,b)) return cnt;
        res+=a;
        cnt+=1;
        if(solve(res,b)) return cnt;
        return -1;
    }
};
28460