CF2192A.String Rotation Game

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Define a block in a string as a contiguous substring of characters of the same type that cannot be extended either to the left or the right. For example, in the string aabcccdaa, there are five blocks:

  • aa (11-st to 22-nd characters)
  • b (33-rd character)
  • ccc (44-th to 66-th characters)
  • d (77-th character)
  • aa (88-th to 99-th characters).

You are playing a game where you are given a string ss of length nn. You can cyclically rotate∗^{\text{∗}} the string however you want. Your score is then calculated as the number of blocks in the final string. Please find the maximum score possible.

∗^{\text{∗}}Formally, choose an index 1≤i≤n1 \leq i \leq n, and replace the string s1s2…sns_1s_2\ldots s_n with the string si+1si+2…sns1s2…sis_{i+1}s_{i+2}\ldots s_ns_1s_2\ldots s_{i}. For example, the string abcde can be rotated to string deabc by choosing i=3i=3.

将字符串中的“块”定义为:由相同类型字符组成的连续子串,且该子串无法向左或向右扩展。例如,在字符串 aabcccdaa 中,共有五个块:

  • aa(第 11 至 22 个字符)
  • b(第 33 个字符)
  • ccc(第 44 至 66 个字符)
  • d(第 77 个字符)
  • aa(第 88 至 99 个字符)。

你正在玩一个游戏:给定一个长度为 nn 的字符串 ss,你可以对它进行任意次数的循环移位∗^{\text{∗}}。你的得分为最终字符串中块的数量。请找出可能获得的最大得分。

∗^{\text{∗}}形式化定义:选择一个下标 1≤i≤n1 \leq i \leq n,将原字符串 s1s2…sns_1s_2\ldots s_n 替换为 si+1si+2…sns1s2…sis_{i+1}s_{i+2}\ldots s_ns_1s_2\ldots s_{i}。例如,字符串 abcde 可通过选取 i=3i=3 移位为 deabc。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001 \le t \le 500). The description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1001 \le n \le 100).

The second line of each test case contains the string ss of length nn.

Strings ss consist of lowercase Latin characters only.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤5001 \le t \le 500)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1001 \le n \le 100)。

每个测试用例的第二行包含长度为 nn 的字符串 ss。

字符串 ss 仅由小写拉丁字母组成。

输出格式

For each testcase, output a single integer denoting the maximum score you can achieve.

对于每个测试用例,输出一个整数,表示你能获得的最高得分。

输入输出样例

  • 输入#1

    4
    4
    abcd
    4
    abbc
    4
    abba
    6
    abbccc

    输出#1

    4
    4
    3
    4

说明/提示

In the first test case, score of the original string abcd is 44. It can be shown that a score greater than 44 cannot be achieved.

In the second test case, cyclically rotating the string by 22 positions will give us string bcab. Score of this string is 44. It can be shown that a score greater than 44 cannot be achieved.

在第一个测试用例中,原字符串 abcd 的得分为 44。可以证明无法得到大于 44 的得分。

在第二个测试用例中,将字符串循环右移 22 位可得到字符串 bcab,其得分为 44。可以证明无法得到大于 44 的得分。

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

首页