#abc272f. F - Two Strings
F - Two Strings
Score : points
问题描述
给定两个长度均为 的由小写英文字母组成的字符串 和 。
对于一个字符串 和一个整数 ,令 表示对 执行以下操作 次后得到的字符串:
- 移除 的第一个字符,并将该字符添加到 的末尾。
找出满足条件 的整数对 的数量,使得 在字典序上小于等于 。
什么是字典序?
简单来说,字典序是字典中单词排列的顺序。让我们通过描述比较两个不同的、由小写英文字母组成的字符串 和 的排序算法来正式定义它。
这里,“字符串 的第 个字符”表示为 。同时,我们写 和 分别表示 在字典序上小于和大于 。
- 设 为 和 长度中的较小值。对于 ,我们检查 是否等于 。
- 如果存在某个 使得 ,则令 为最小的这样的 。比较 和 ,若按照字母顺序 小于 ,则终止算法并确定 ;否则确定 。
- 若不存在使 的 ,通过比较 和 的长度来终止算法:若 比 短,则确定 ;否则确定 。
以上为通义千问 qwen-max 翻译,仅供参考。
Problem Statement
You are given strings and of length each, consisting of lowercase English letters.
For a string and an integer , let be the string obtained by performing on the following operation times:
- Remove the first character of and append the same character to the end of .
Find the number of integer pairs satisfying such that is lexicographically smaller than or equal to .
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 and consisting of lowercase English letters.
Here, we denote "the -th character of a string " by . Also, we write and if is lexicographically smaller and larger than , respectively.
- Let be the smallest of the lengths of and . For , we check if equals .
- If there is an such that , let be the smallest such . Comparing and , we terminate the algorithm by determining that if is smaller than in the alphabetical order, and that otherwise.
- If there is no such that , comparing the lengths of and , we terminate the algorithm by determining that if is shorter than , and that otherwise.
Constraints
- and are strings of length each, consisting of lowercase English letters.
- is an integer.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1
3
adb
cab
Sample Output 1
4
There are pairs of satisfying the conditions: , and .
does not satisfy the conditions because dba
and bca
.
Sample Input 2
10
wsiuhwijsl
pwqoketvun
Sample Output 2
56
update @ 2024/3/10 11:28:29