intfindStartIndexOfSubstring(string s1, string s2){ int n = s1.length(), m = s2.length(); if(n == m) return s1 == s2 ? 0 : -1; if(n < m) return-1;
longlong base = 1010101, mod = 1e9 + 7, d = 1; longlong target = 0, hash = 0; for(int i = 0; i < m; i++) { target = (target * base + s2[i]) % mod; d = d * base % mod; } for(int i = 0; i < n; i++) { hash = (hash * base + s1[i]) % mod; if(i >= m) hash = (hash + mod - d * s1[i - m] % mod) % mod; if(i >= m - 1and hash == target and s1.substr(i - m + 1, m) == s2) return i - m + 1; } return-1; }