#include<stdio.h>
#define MAX_LONG 250001
int N;
char S[MAX_LONG * 2];
char T[MAX_LONG];
int next[MAX_LONG];
int main(){
scanf("%d %s %s", &N, S, T);
int len = N * 2;
int i;
for (i = 0; i < N; i++){
S[N + i] = S[i];
}
S[N + i] = '\0';
int prefix = -1;
int suffix = 0;
next[suffix] = -1;
while (suffix < N - 1){
if (-1 == prefix || T[prefix] == T[suffix]){
prefix++;
suffix++;
if (T[prefix] != T[suffix]){
next[suffix] = prefix;
} else {
next[suffix] = next[prefix];
}
} else {
prefix = next[prefix];
}
}
int SIndex = 0;
int TIndex = 0;
while (SIndex < len && TIndex < N){
if (S[SIndex] == T[TIndex] || -1 == TIndex){
SIndex++;
TIndex++;
} else {
TIndex = next[TIndex];
}
}
int shift = len - SIndex;
if (N == shift){
printf("0\n");
} else if (0 == shift){
printf("-1\n");
} else {
printf("%d\n", shift);
}
return 0;
}
URAL 1423 String Tale
最新推荐文章于 2018-06-23 12:53:03 发布