CF1552A.Subsequence Permutation

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A string ss of length nn, consisting of lowercase letters of the English alphabet, is given.

You must choose some number kk between 00 and nn. Then, you select kk characters of ss and permute them however you want. In this process, the positions of the other n−kn-k characters remain unchanged. You have to perform this operation exactly once.

For example, if s="andrea"s=\texttt{"andrea"}, you can choose the k=4k=4 characters \texttt{"a_d_ea"} and permute them into \texttt{"d_e_aa"} so that after the operation the string becomes "dneraa"\texttt{"dneraa"}.

Determine the minimum kk so that it is possible to sort ss alphabetically (that is, after the operation its characters appear in alphabetical order).

给定一个长度为 nn 的字符串 ss,它仅由小写英文字母组成。

你需要选择一个介于 00 和 nn 之间的整数 kk。然后,从 ss 中选出 kk 个字符,并以任意方式对其重排(即置换)。在此过程中,其余 n−kn-k 个字符的位置保持不变。你必须恰好执行一次该操作。

例如,若 s="andrea"s=\texttt{"andrea"},你可以选择 k=4k=4 个字符 \texttt{"a_d_ea"}(下划线表示保留原位置的其他字符),并将它们重排为 \texttt{"d_e_aa"},使得操作后字符串变为 "dneraa"\texttt{"dneraa"}。

请确定最小的 kk,使得通过上述操作能够将 ss 按字母序排序(即操作后其字符按字典序非降序排列)。

输入格式

The first line contains a single integer tt (1≤t≤10001 \le t \le 1000) — the number of test cases. Then tt test cases follow.

The first line of each test case contains one integer nn (1≤n≤401 \le n \le 40) — the length of the string.

The second line of each test case contains the string ss. It is guaranteed that ss contains only lowercase letters of the English alphabet.

第一行包含一个整数 tt(1≤t≤10001 \le t \le 1000)—— 表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例的第一行包含一个整数 nn(1≤n≤401 \le n \le 40)—— 表示字符串的长度。

每个测试用例的第二行包含字符串 ss。保证 ss 仅由英文字母的小写字母组成。

输出格式

For each test case, output the minimum kk that allows you to obtain a string sorted alphabetically, through the operation described above.

对于每个测试用例,输出能够通过上述操作得到按字母顺序排序的字符串的最小 kk 值。

输入输出样例

  • 输入#1

    4
    3
    lol
    10
    codeforces
    5
    aaaaa
    4
    dcba

    输出#1

    2
    6
    0
    4

说明/提示

In the first test case, we can choose the k=2k=2 characters \texttt{"_ol"} and rearrange them as \texttt{"_lo"} (so the resulting string is "llo"\texttt{"llo"}). It is not possible to sort the string choosing strictly less than 22 characters.

In the second test case, one possible way to sort ss is to consider the k=6k=6 characters \texttt{"_o__force_"} and rearrange them as \texttt{"_c__efoor_"} (so the resulting string is "ccdeefoors"\texttt{"ccdeefoors"}). One can show that it is not possible to sort the string choosing strictly less than 66 characters.

In the third test case, string ss is already sorted (so we can choose k=0k=0 characters).

In the fourth test case, we can choose all k=4k=4 characters "dcba"\texttt{"dcba"} and reverse the whole string (so the resulting string is "abcd"\texttt{"abcd"}).

在第一个测试用例中,我们可以选择 k=2k=2 个字符 \texttt{"_ol"},并将它们重排为 \texttt{"_lo"}(因此得到的字符串为 "llo"\texttt{"llo"})。无法通过选择严格少于 22 个字符来将该字符串排序。

在第二个测试用例中,一种将 ss 排序的可行方法是:选择 k=6k=6 个字符 \texttt{"_o__force_"},并将它们重排为 \texttt{"_c__efoor_"}(因此得到的字符串为 "ccdeefoors"\texttt{"ccdeefoors"})。可以证明,无法通过选择严格少于 66 个字符来将该字符串排序。

在第三个测试用例中,字符串 ss 已经是有序的(因此我们可以选择 k=0k=0 个字符)。

在第四个测试用例中,我们可以选择全部 k=4k=4 个字符 "dcba"\texttt{"dcba"},并反转整个字符串(因此得到的字符串为 "abcd"\texttt{"abcd"})。

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

首页