CF1905C.Largest Subsequence

普及/提高-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Given is a string ss of length nn. In one operation you can select the lexicographically largest†^\dagger subsequence of string ss and cyclic shift it to the right‡^\ddagger.

Your task is to calculate the minimum number of operations it would take for ss to become sorted, or report that it never reaches a sorted state.

†^\daggerA string aa is lexicographically smaller than a string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba \ne b;
  • In the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

‡^\ddaggerBy cyclic shifting the string t1t2…tmt_1t_2\ldots t_m to the right, we get the string tmt1…tm−1t_mt_1\ldots t_{m-1}.

给定一个长度为 nn 的字符串 ss。在一次操作中,你可以选择字符串 ss 的字典序最大†^\dagger的子序列,并将其向右循环移位‡^\ddagger。

你的任务是计算使 ss 变为升序排列所需的最少操作次数;若 ss 永远无法变为升序排列,则报告这一点。

†^\dagger 字符串 aa 字典序小于字符串 bb,当且仅当以下条件之一成立:

  • aa 是 bb 的前缀,但 a≠ba \ne b;
  • 在 aa 与 bb 首次出现不同字符的位置上,aa 中该位置的字母在字母表中早于 bb 中对应位置的字母。

‡^\ddagger 将字符串 t1t2…tmt_1t_2\ldots t_m 向右循环移位后,得到字符串 tmt1…tm−1t_mt_1\ldots t_{m-1}。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the length of the string ss.

The second line of each test case contains a single string ss of length nn, consisting of lowercase English letters.

It is guaranteed that sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5),表示字符串 ss 的长度。

每个测试用例的第二行包含一个长度为 nn 的字符串 ss,由小写英文字母组成。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output a single integer — the minimum number of operations required to make ss sorted, or −1-1 if it's impossible.

对于每个测试用例,输出一个整数——使 ss 变为有序所需的最少操作次数;如果不可能,则输出 −1-1。

输入输出样例

  • 输入#1

    6
    5
    aaabc
    3
    acb
    3
    bac
    4
    zbca
    15
    czddeneeeemigec
    13
    cdefmopqsvxzz

    输出#1

    0
    1
    -1
    2
    6
    0

说明/提示

In the first test case, the string ss is already sorted, so we need no operations.

In the second test case, doing one operation, we will select cb and cyclic shift it. The string ss is now abc which is sorted.

In the third test case, ss cannot be sorted.

In the fourth test case we will perform the following operations:

  • The lexicographically largest subsequence is zca. Then ss becomes abzc.
  • The lexicographically largest subsequence is zc. Then ss becomes abcz. The string becomes sorted.

Thus, we need 22 operations.

在第一个测试用例中,字符串 ss 已经是有序的,因此无需任何操作。

在第二个测试用例中,执行一次操作:我们选择子串 cb 并对其作循环移位,字符串 ss 变为 abc,此时已有序。

在第三个测试用例中,字符串 ss 无法被排序。

在第四个测试用例中,我们将执行以下操作:

  • 字典序最大的子序列是 zca,于是 ss 变为 abzc。
  • 字典序最大的子序列是 zc,于是 ss 变为 abcz,此时字符串已有序。

因此,总共需要 22 次操作。

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

首页