<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>

#define M 101
class Solution {
    int __rabinKarp(string &text, string &pat) {
        int n=text.size(),m=pat.size();
        int j=0;
        for(int i=0;i<n;i++) {
            if(text[i]==pat[j]) {
                int ans=i;
                int k=0;
                while(k<m) {
                    if(text[i+k]!=pat[k]) {
                        break;
                    }
                    k++;
                }
                if(k==m) {
                    return ans;
                }
            }
        }
        return -1;
    }
    int __rabinKarp2(string &text, string &pat) {
        if(text=="" || pat=="") return -1;
        int n=text.size(),m=pat.size();
        int p=1,t=0;
        for(int i=0;i<m;i++) {
            p=(p*101)%M;
            t=(t*101+pat[i])%M;
        }
        int h=0;
        // for(int i=0;i<n;i++) {
        //     if(i<m-1) continue;
        //     if(i>=m) h=(h-text[i-m]*p)%M;
        //     if(h<0) h+=M;
        //     if(h==t && text.substr(i-m+1,m)==pat) return i-m+1; 
        // }
        for(int i = 0;i<text.size();i++){
            h = (h*101 + text[i])%M;
            if(i<m-1) continue;
            if(i>=m){
                h = (h-text[i-m]*p)%M;
            }
            if(h<0)
                h+=M;
            if(h == t){
                if(text.substr(i-m+1,m) == pat)
                    return i-m+1;
            }
        }
        return -1;
    }
public:
    int repeatedStringMatch(string a, string b) {
        if(a==b) return 1;
        string s=a;
        int cnt=1;
        while(s.length()<b.length()) {cnt++;s+=a;}
        if(s==b) return cnt;
        if(__rabinKarp2(s,b)!=-1) return cnt;
        s+=a;
        if(__rabinKarp2(s,b)!=-1) return cnt+1;
        return -1;
    }
};
322300