CF1660C.Get an Even String

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

A string a=a1a2…ana=a_1a_2\dots a_n is called even if it consists of a concatenation (joining) of strings of length 22 consisting of the same characters. In other words, a string aa is even if two conditions are satisfied at the same time:

  • its length nn is even;
  • for all odd ii (1≤i≤n−11 \le i \le n - 1), ai=ai+1a_i = a_{i+1} is satisfied.

For example, the following strings are even: "" (empty string), "tt", "aabb", "oooo", and "ttrrrroouuuuuuuukk". The following strings are not even: "aaa", "abab" and "abba".

Given a string ss consisting of lowercase Latin letters. Find the minimum number of characters to remove from the string ss to make it even. The deleted characters do not have to be consecutive.

如果一个字符串 a=a1a2…ana=a_1a_2\dots a_n 可以表示为若干个长度为 22、且由相同字符组成的子串的拼接(连接),则称该字符串为偶字符串。换言之,字符串 aa 是偶字符串当且仅当同时满足以下两个条件:

  • 其长度 nn 为偶数;
  • 对所有奇数下标 ii(1≤i≤n−11 \le i \le n - 1),均有 ai=ai+1a_i = a_{i+1}。

例如,以下字符串是偶字符串:""(空字符串)、"tt"、"aabb"、"oooo" 和 "ttrrrroouuuuuuuukk";而以下字符串不是偶字符串:"aaa"、"abab" 和 "abba"。

给定一个仅由小写拉丁字母组成的字符串 ss。求使 ss 变为偶字符串所需删除的最少字符个数。被删除的字符不必连续。

输入格式

The first line of input data contains an integer tt (1≤t≤1041 \le t \le 10^4) —the number of test cases in the test.

The descriptions of the test cases follow.

Each test case consists of one string ss (1≤∣s∣≤2⋅1051 \le |s| \le 2 \cdot 10^5), where ∣s∣|s| — the length of the string ss. The string consists of lowercase Latin letters.

It is guaranteed that the sum of ∣s∣|s| on all test cases does not exceed 2⋅1052 \cdot 10^5.

输入数据的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)——表示测试用例的数量。

随后是各测试用例的描述。

每个测试用例由一个字符串 ss 组成(1≤∣s∣≤2⋅1051 \le |s| \le 2 \cdot 10^5),其中 ∣s∣|s| 表示字符串 ss 的长度。该字符串仅由小写拉丁字母组成。

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

输出格式

For each test case, print a single number — the minimum number of characters that must be removed to make ss even.

对于每个测试用例,输出一个整数——使字符串 ss 变为偶串所需删除的最少字符数。

输入输出样例

  • 输入#1

    6
    aabbdabdccc
    zyx
    aaababbb
    aabbcc
    oaoaaaoo
    bmefbmuyw

    输出#1

    3
    3
    2
    0
    2
    7

说明/提示

In the first test case you can remove the characters with indices 66, 77, and 99 to get an even string "aabbddcc".

In the second test case, each character occurs exactly once, so in order to get an even string, you must remove all characters from the string.

In the third test case, you can get an even string "aaaabb" by removing, for example, 44-th and 66-th characters, or a string "aabbbb" by removing the 55-th character and any of the first three.

在第一个测试用例中,你可以删除索引为 66、77 和 99 的字符,从而得到偶字符串 "aabbddcc"。

在第二个测试用例中,每个字符恰好出现一次,因此为了得到偶字符串,你必须从字符串中删除所有字符。

在第三个测试用例中,你可以通过删除例如第 44 个和第 66 个字符,得到偶字符串 "aaaabb";或者通过删除第 55 个字符以及前三个字符中的任意一个,得到偶字符串 "aabbbb"。

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

首页