#abc272f. F - Two Strings

F - Two Strings

Score : 500500 points

问题描述

给定两个长度均为 NN 的由小写英文字母组成的字符串 SSTT

对于一个字符串 XX 和一个整数 ii,令 f(X,i)f(X,i) 表示对 XX 执行以下操作 ii 次后得到的字符串:

  • 移除 XX 的第一个字符,并将该字符添加到 XX 的末尾。

找出满足条件 0i,jN10 \le i,j \le N-1 的整数对 (i,j)(i,j) 的数量,使得 f(S,i)f(S,i) 在字典序上小于等于 f(T,j)f(T,j)

什么是字典序?

简单来说,字典序是字典中单词排列的顺序。让我们通过描述比较两个不同的、由小写英文字母组成的字符串 SSTT 的排序算法来正式定义它。

这里,“字符串 SS 的第 ii 个字符”表示为 SiS_i。同时,我们写 S<TS \lt TS>TS \gt T 分别表示 SS 在字典序上小于和大于 TT

  1. LLSSTT 长度中的较小值。对于 i=1,2,,Li=1,2,\dots,L,我们检查 SiS_i 是否等于 TiT_i
  2. 如果存在某个 ii 使得 SiTiS_i \neq T_i,则令 jj 为最小的这样的 ii。比较 SjS_jTjT_j,若按照字母顺序 SjS_j 小于 TjT_j,则终止算法并确定 S<TS \lt T;否则确定 S>TS \gt T
  3. 若不存在使 SiTiS_i \neq T_iii,通过比较 SSTT 的长度来终止算法:若 SSTT 短,则确定 S<TS \lt T;否则确定 S>TS \gt T

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

3
adb
cab

Sample Output 1

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.

Sample Input 2

10
wsiuhwijsl
pwqoketvun

Sample Output 2

56

update @ 2024/3/10 11:28:29