#ABC272F. [ABC272F] Two Strings

[ABC272F] Two Strings

Score : 500500 points

Problem Statement

You are given strings SS and TT of length NN each, consisting of lowercase English letters.

For a string XX and an integer ii, let f(X,i)f(X,i) be the string obtained by performing on XX the following operation ii times:

  • Remove the first character of XX and append the same character to the end of XX.

Find the number of integer pairs (i,j)(i,j) satisfying 0i,jN10 \le i,j \le N-1 such that f(S,i)f(S,i) is lexicographically smaller than or equal to f(T,j)f(T,j).

What is the lexicographical order?

Simply put, the lexicographical order is the order in which the words are arranged in a dictionary. Let us define it formally by describing an algorithm of finding the ordering of two distinct strings SS and TT consisting of lowercase English letters.

Here, we denote "the ii-th character of a string SS" by SiS_i. Also, we write S<TS \lt T and S>TS \gt T if SS is lexicographically smaller and larger than TT, respectively.

  1. Let LL be the smallest of the lengths of SS and TT. For i=1,2,,Li=1,2,\dots,L, we check if SiS_i equals TiT_i.
  2. If there is an ii such that SiTiS_i \neq T_i, let jj be the smallest such ii. Comparing SjS_j and TjT_j, we terminate the algorithm by determining that S<TS \lt T if SjS_j is smaller than TjT_j in the alphabetical order, and that S>TS \gt T otherwise.
  3. If there is no ii such that SiTiS_i \neq T_i, comparing the lengths of SS and TT, we terminate the algorithm by determining that S<TS \lt T if SS is shorter than TT, and that S>TS \gt T otherwise.

Constraints

  • 1N2×1051 \le N \le 2 \times 10^5
  • SS and TT are strings of length NN each, consisting of lowercase English letters.
  • NN is an integer.

Input

The input is given from Standard Input in the following format:

NN

SS

TT

Output

Print the answer.

3
adb
cab
4

There are 44 pairs of (i,j)(i, j) satisfying the conditions: (0,0),(0,2),(2,0)(0,0),(0,2),(2,0), and (2,2)(2,2).

(i,j)=(1,2)(i,j)=(1,2) does not satisfy the conditions because f(S,i)=f(S,i)=dba and f(T,j)=f(T,j)=bca.

10
wsiuhwijsl
pwqoketvun
56