#arc175f. F - Append Same Characters
F - Append Same Characters
Score: points
问题陈述
给定 个字符串 ,由小写英文字母组成。考虑执行以下两种类型的操作,每种操作可以执行零次或多次,且执行顺序任意:
- 选择一个小写字母 。将 附加到所有 的 的末尾。
- 选择一个整数 ,使得 。交换 和 。
找到使 在字典序上对所有 成立的最小总操作数。
什么是字典序?
如果满足以下 1. 或 2. 中的任一条件,我们说字符串 字典序上小于字符串 。这里, 和 分别表示 和 的长度。
- 且 。
- 存在一个整数 使得以下两个条件同时成立:
- 。
- 字母 在字母表顺序上先于 。
以上为大语言模型 kimi 翻译,仅供参考。
Problem Statement
You are given strings consisting of lowercase English letters. Consider performing the following two types of operations zero or more times in any order:
- Choose one lowercase letter . Append to the end of for all .
- Choose an integer such that . Swap and .
Find the minimum total number of operations required to make in lexicographical order for all .
What is lexicographical order?
A string is said to be lexicographically smaller than a string if 1. or 2. below holds. Here, and denote the lengths of and , respectively.
- and .
- There is an integer such that both of the following hold:
- .
- The letter comes before in alphabetical order.
Constraints
- All input values are integers.
- is a string consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
Output
Print the answer in a single line.
Sample Input 1
5
ab
rac
a
dab
ra
Sample Output 1
3
Here is one way to operate.
- Swap and . Now
ab
,a
,rac
,dab
,ra
. - Append
z
to the end of each string. Nowabz
,az
,racz
,dabz
,raz
. - Swap and . Now
abz
,az
,dabz
,racz
,raz
. At this point, we have for all .
It is impossible to make for all with fewer than three operations, so you should print .
Sample Input 2
3
kitekuma
nok
zkou
Sample Output 2
0
Before any operation is performed, we have for all .
Sample Input 3
31
arc
arrc
rc
rac
a
rc
aara
ra
caac
cr
carr
rrra
ac
r
ccr
a
c
aa
acc
rar
r
c
r
a
r
rc
a
r
rc
cr
c
Sample Output 3
175
Note that we may have for .