AT_agc078_a.Rearrange ABC
NOI/NOI+/CTSC
通过率:0%
时间限制:2.00s
内存限制:1024MB
AC君温馨提醒
该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given strings S and T of length 3N, each containing exactly N occurrences of each of A, B, and C.
You can perform the following operation zero or more times.
- Choose one substring of S whose characters are all distinct.
- Sort the characters of the chosen substring in ascending order.
Determine whether it is possible to make S equal to T, and if it is possible, find the minimum number of operations required.
给你两个长度为 3N 的字符串 S 和 T,其中每个字符串都恰好包含 N 个字符 'A'、N 个字符 'B' 和 N 个字符 'C'。
你可以执行以下操作零次或多次:
- 选择 S 的一个子串,要求该子串中所有字符互不相同;
- 将所选子串中的字符按升序排序。
判断是否可能通过若干次操作使 S 变为 T;若可能,求出所需的最少操作次数。
输入格式
The input is given from Standard Input in the following format:
N
S
T
输入从标准输入中按以下格式给出:
N
S
T
输出格式
Output the answer. If it is impossible to make S equal to T, output -1.
输出答案。如果无法使 S 等于 T,则输出 -1。
输入输出样例
输入#1
2 CACBAB ACABCB
输出#1
2
输入#2
1 ABC CBA
输出#2
-1
输入#3
8 CCBCCCABCCAABBBACABAAABB BCAAACCABCCBACBBBACAABCB
输出#3
32
说明/提示
Sample 1 Explanation:
For example, you can make S equal to T by performing two operations as follows.
- Choose the first two characters of
CACBAB, making itACCBAB. - Choose the 3-rd through 5-th characters of
ACCBAB, making itACABCB.
It is impossible to make them equal with one operation, so the answer is 2.
Sample 2 Explanation:
No matter how you perform the operations, you cannot make ABC equal to CBA.
Constraints
- 1≤N≤100
- S and T are strings of length 3N consisting of
A,B, andC. - Each of S and T contains exactly N occurrences of each of
A,B, andC. - All input values are integers.
样例 1 解释:
例如,你可以通过执行以下两次操作使 S 变为 T:
- 选择字符串
CACBAB的前两个字符,将其变为ACCBAB; - 选择字符串
ACCBAB的第 3 至第 5 个字符,将其变为ACABCB。
无法仅通过一次操作使二者相等,因此答案为 2。
样例 2 解释:
无论怎样执行操作,都无法将 ABC 变为 CBA。
约束条件
- 1≤N≤100
- S 和 T 均为长度为 3N 的字符串,且仅由字符
A、B和C组成。 - S 和 T 中各自恰好包含 N 个
A、N 个B和 N 个C。 - 所有输入值均为整数。
输入解题思路,AI测评打分。不知道怎么写?