P27089 [AtCoder] Circulation
NOI
题目描述

Problem Description

Given two strings \( S \) and \( T \) of length \( N \) consisting of 0 and 1.

Consider the following operation:

  • Choose an integer \( x \) such that \( 1 \leq x \leq N \), and split the string \( S = S_1 \ldots S_N \) into \( S_1 \ldots S_x \) and \( S_{x+1} \ldots S_N \), then perform a cyclic right shift on each part and concatenate them.

A cyclic right shift of a string \( U = U_1 \ldots U_M \) means changing \( U_1 \ldots U_{M-1}U_M \) to \( U_MU_1 \ldots U_{M-1} \). When \( x = N \), the entire string \( S \) is cyclically right-shifted. For example, if the string \( S = 01001 \) and \( x = 3 \), the string is first split into \( 010 \) and \( 01 \), which after cyclic right shifts become \( 001 \) and \( 10 \), resulting in the string \( 00110 \).

Determine if it is possible to make the string \( S \) match the string \( T \) by performing this operation any number of times. If it is not possible, output MuriyarokonnNaN in one line. If it is possible, output the minimum number of operations required in one line.

Input Format

The input is given from the standard input in the following format:

\( N \) \( S \) \( T \)

Output Format

If it is possible to make the string \( S \) match the string \( T \) by performing the operation any number of times, output the minimum number of operations required. If it is not possible, output MuriyarokonnNaN, each in one line.

Sample Input and Output

Sample Input #1

6
010101
010110

Sample Output #1

3

Sample Input #2

18
111101101001101001
111101101001101001

Sample Output #2

0

Sample Input #3

18
011110100101000100
000110010011100110

Sample Output #3

8

Notes/Hints

Constraints

  • \( N \) is an integer satisfying \( 1 \leq N \leq 10^5 \)
  • \( S \) and \( T \) are strings consisting of 0 and 1
  • \( |S| = |T| = N \)
  • The number of 0s in \( S \) is equal to the number of 0s in \( T \)
  • The number of 1s in \( S \) is equal to the number of 1s in \( T \)

Sample Explanation 1

The string \( S \) can be made to match the string \( T \) by performing the following sequence of operations:
- Perform the operation with \( x = 1 \)
- Perform the operation with \( x = 5 \)
- Perform the operation with \( x = 6 \)
At each step, the string \( S \) changes as follows: \( 010101 \to 011010 \to 101100 \to 010110 \).

Sample Explanation 2

No operations are needed as the strings already match.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
6 010101 010110
样例输出
3