AT_agc078_a.Rearrange ABC

NOI/NOI+/CTSC

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given strings SS and TT of length 3N3N, each containing exactly NN occurrences of each of A, B, and C.

You can perform the following operation zero or more times.

  • Choose one substring of SS whose characters are all distinct.
  • Sort the characters of the chosen substring in ascending order.

Determine whether it is possible to make SS equal to TT, and if it is possible, find the minimum number of operations required.

给你两个长度为 3N3N 的字符串 SS 和 TT,其中每个字符串都恰好包含 NN 个字符 'A'、NN 个字符 'B' 和 NN 个字符 'C'。

你可以执行以下操作零次或多次:

  • 选择 SS 的一个子串,要求该子串中所有字符互不相同;
  • 将所选子串中的字符按升序排序。

判断是否可能通过若干次操作使 SS 变为 TT;若可能,求出所需的最少操作次数。

输入格式

The input is given from Standard Input in the following format:

NN
SS
TT

输入从标准输入中按以下格式给出:

NN
SS
TT

输出格式

Output the answer. If it is impossible to make SS equal to TT, output -1.

输出答案。如果无法使 SS 等于 TT,则输出 -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 SS equal to TT by performing two operations as follows.

  • Choose the first two characters of CACBAB, making it ACCBAB.
  • Choose the 33-rd through 55-th characters of ACCBAB, making it ACABCB.

It is impossible to make them equal with one operation, so the answer is 22.

Sample 2 Explanation:
No matter how you perform the operations, you cannot make ABC equal to CBA.

Constraints

  • 1≤N≤1001 \le N \le 100
  • SS and TT are strings of length 3N3N consisting of A, B, and C.
  • Each of SS and TT contains exactly NN occurrences of each of A, B, and C.
  • All input values are integers.

样例 1 解释:
例如,你可以通过执行以下两次操作使 SS 变为 TT:

  • 选择字符串 CACBAB 的前两个字符,将其变为 ACCBAB;
  • 选择字符串 ACCBAB 的第 33 至第 55 个字符,将其变为 ACABCB。

无法仅通过一次操作使二者相等,因此答案为 22。

样例 2 解释:
无论怎样执行操作,都无法将 ABC 变为 CBA。

约束条件

  • 1≤N≤1001 \le N \le 100
  • SS 和 TT 均为长度为 3N3N 的字符串,且仅由字符 A、B 和 C 组成。
  • SS 和 TT 中各自恰好包含 NN 个 A、NN 个 B 和 NN 个 C。
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页