Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- typedef long long ll;
- using namespace std;
- #define f(i,a,b) for(int i = (a); i <= (b); i++)
- const ll MOD = 1000000007;
- int F[250005], N;
- string S, T;
- int main()
- {
- cin >> N >> S >> T;
- S += S;
- F[0] = -1, F[1] = 0;
- f(i,2,N)
- {
- int match = F[i-1];
- while(match >= 0 && T[match] != T[i-1]) match = F[match];
- F[i] = match+1;
- }
- int match = 0;
- f(i,0,2*N-1)
- {
- while(match >= 0 && S[i] != T[match]) match = F[match];
- match++;
- if(match == N)
- {
- if(i == N-1) cout << 0;
- else cout << 2*N-i-1;
- return 0;
- }
- }
- cout << -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement