103452: [Atcoder]ABC345 C - One Time Swap
Description
Points: $350$ points
Problem Statement
You are given a string $S$. Find the number of strings that can result from performing the following operation exactly once.
- Let $N$ be the length of $S$. Choose a pair of integers $(i,j)$ such that $1\leq i<j\leq N$ and swap the $i$-th and $j$-th characters of $S$.
It can be proved that you can always perform it under the constraints of this problem.
Constraints
- $S$ is a string of length between $2$ and $10^6$, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
$S$
Output
Print the number of strings that can result from performing the operation in the problem statement exactly once on $S$.
Sample Input 1
abc
Sample Output 1
3
The length of $S$ is $3$, so $1\leq i<j\leq 3$ is satisfied by three pairs of integers $(i,j)$: $(1,2)$, $(1,3)$, and $(2,3)$.
- Swapping the $1$-st and $2$-nd characters of $S$ results in $S$ being
bac
. - Swapping the $1$-st and $3$-rd characters of $S$ results in $S$ being
cba
. - Swapping the $2$-nd and $3$-rd characters of $S$ results in $S$ being
acb
.
Therefore, the operation on abc
results in one of the three strings: bac
, cba
, and acb
, so print $3$.
Sample Input 2
aaaaa
Sample Output 2
1
Swapping any two characters results in $S$ remaining aaaaa
. Thus, only one string can result from the operation.
Input
Output
问题描述
给定一个字符串$S$。找出执行以下操作恰好一次后可以得到的字符串的数量。
- 令$N$为$S$的长度。选择一对整数$(i,j)$,使得$1\leq i<j\leq N$,并将$S$中的第$i$个字符和第$j$个字符交换。
可以证明,在本问题的约束条件下,你总是可以执行此操作。
约束条件
- $S$是一个长度在$2$到$10^6$之间的字符串,由小写英文字母组成。
输入
输入通过标准输入给出以下格式:
$S$
输出
打印在问题描述中的操作恰好执行一次后,$S$可以得到的字符串的数量。
样例输入1
abc
样例输出1
3
$S$的长度为$3$,因此满足$1\leq i<j\leq 3$的整数对$(i,j)$有三个:$(1,2)$,$(1,3)$和$(2,3)$。
- 将$S$中的第$1$个字符和第$2$个字符交换,得到的结果字符串为$S$为
bac
。 - 将$S$中的第$1$个字符和第$3$个字符交换,得到的结果字符串为$S$为
cba
。 - 将$S$中的第$2$个字符和第$3$个字符交换,得到的结果字符串为$S$为
acb
。
因此,在abc
上执行操作可以得到三个字符串之一:bac
,cba
和acb
,因此打印$3$。
样例输入2
aaaaa
样例输出2
1
交换任何两个字符都会使$S$保持为aaaaa
。因此,操作只能得到一个字符串。