CF1332C.K-Complete Word

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

Word ss of length nn is called kk-complete if

  • ss is a palindrome, i.e. si=sn+1−is_i=s_{n+1-i} for all 1≤i≤n1 \le i \le n;
  • ss has a period of kk, i.e. si=sk+is_i=s_{k+i} for all 1≤i≤n−k1 \le i \le n-k.

For example, "abaaba" is a 33-complete word, while "abccba" is not.

Bob is given a word ss of length nn consisting of only lowercase Latin letters and an integer kk, such that nn is divisible by kk. He wants to convert ss to any kk-complete word.

To do this Bob can choose some ii (1≤i≤n1 \le i \le n) and replace the letter at position ii with some other lowercase Latin letter.

So now Bob wants to know the minimum number of letters he has to replace to convert ss to any kk-complete word.

Note that Bob can do zero changes if the word ss is already kk-complete.

You are required to answer tt test cases independently.

长度为 nn 的字符串 ss 被称为 kk-完全的,当且仅当满足以下两个条件:

  • ss 是一个回文串,即对所有 1≤i≤n1 \le i \le n,均有 si=sn+1−is_i = s_{n+1-i};
  • ss 具有周期 kk,即对所有 1≤i≤n−k1 \le i \le n-k,均有 si=sk+is_i = s_{k+i}。

例如,"abaaba" 是一个 33-完全的字符串,而 "abccba" 则不是。

Bob 被给定一个长度为 nn 的字符串 ss(仅由小写拉丁字母组成)以及一个整数 kk,其中 nn 可被 kk 整除。他希望将 ss 转换为任意一个 kk-完全的字符串。

为此,Bob 可以选择某个位置 ii(1≤i≤n1 \le i \le n),并将该位置上的字符替换为另一个小写拉丁字母。

现在 Bob 想知道:将 ss 转换为任意一个 kk-完全字符串所需的最少字符替换次数是多少?

注意:若字符串 ss 本身已是 kk-完全的,则 Bob 可以不进行任何修改(即替换次数为 00)。

你需要独立地回答 tt 组测试用例。

输入格式

The first line contains a single integer tt (1≤t≤1051 \le t\le 10^5) — the number of test cases.

The first line of each test case contains two integers nn and kk (1≤k<n≤2⋅1051 \le k \lt n \le 2 \cdot 10^5, nn is divisible by kk).

The second line of each test case contains a word ss of length nn.

It is guaranteed that word ss only contains lowercase Latin letters. And it is guaranteed that the sum of nn over all test cases will not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k<n≤2⋅1051 \le k \lt n \le 2 \cdot 10^5,且 nn 能被 kk 整除)。

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

保证字符串 ss 仅由小写拉丁字母组成;且保证所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output one integer, representing the minimum number of characters he has to replace to convert ss to any kk-complete word.

对于每个测试用例,输出一个整数,表示将 ss 转换为任意一个 kk-完全单词所需替换的最少字符数。

输入输出样例

  • 输入#1

    4
    6 2
    abaaba
    6 3
    abaaba
    36 9
    hippopotomonstrosesquippedaliophobia
    21 7
    wudixiaoxingxingheclp

    输出#1

    2
    0
    23
    16

说明/提示

In the first test case, one optimal solution is aaaaaa.

In the second test case, the given word itself is kk-complete.

在第一个测试用例中,一个最优解是 aaaaaa。

在第二个测试用例中,给定的单词本身是 kk-完备的。

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

首页