Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int n, m;
- long long sol;
- char a[2000005], b[2000005];
- int pi[2000005], mx[2000005];
- int main() {
- cin >> (a + 1) >> (b + 1);
- n = strlen(a + 1); m = strlen(b + 1);
- int q = 0;
- for(int i = 2; i <= n; i++) {
- while(q && a[q + 1] != a[i])
- q = pi[q];
- if(a[q + 1] == a[i])
- q++;
- pi[i] = q;
- }
- q = 0;
- for(int i = 1; i <= m; i++) {
- while(q && a[q + 1] != b[i])
- q = pi[q];
- if(a[q + 1] == b[i])
- q++;
- int tmp = q;
- while(tmp) {
- mx[i - tmp + 1] = max(mx[i - tmp + 1], tmp);
- tmp = pi[tmp];
- }
- }
- for(int i = 1; i <= m; i++)
- sol += 1LL * (mx[i] + 1) * mx[i] / 2 + 1LL * mx[i] * (m - i - mx[i] + 1);
- cout << sol;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement