K-Complete Word CodeForces - 1332C(贪心)
Word s of length n is called k-complete if
s is a palindrome, i.e. si=sn+1?i for all 1≤i≤n;
s has a period of k, i.e. si=sk+i for all 1≤i≤n?k.
For example, “abaaba” is a 3-complete word, while “abccba” is not.
Bob is given a word s of length n consisting of only lowercase Latin letters and an integer k, such that n is divisible by k. He wants to convert s to any k-complete word.
To do this Bob can choose some i (1≤i≤n) and replace the letter at position i with some other lowercase Latin letter.
So now Bob wants to know the minimum number of letters he has to replace to convert s to any k-complete word.
Note that Bob can do zero changes if the word s is already k-complete.
You are required to answer t test cases independently.
Input
The first line contains a single integer t (1≤t≤105) — the number of test cases.
The first line of each test case contains two integers n and k (1≤k<n≤2?105, n is divisible by k).
The second line of each test case contains a word s of length n.
It is guaranteed that word s only contains lowercase Latin letters. And it is guaranteed that the sum of n over all test cases will not exceed 2?105.
Output
For each test case, output one integer, representing the minimum number of characters he has to replace to convert s to any k-complete word.
Example
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
Output
2
0
23
16
Note
In the first test case, one optimal solution is aaaaaa.
In the second test case, the given word itself is k-complete.
唉,挺簡單的一道題目給整復雜了。。
思路:根據題目要求,我們可以推斷出,給定的字符串,每k位就是一個回文字符串。這樣的話,我們就考慮這k位的字符串。對于這k位的回文字符串,我們要考慮對稱的位置字符的數量,例如第一個樣例來說,abaaba,第一位a有2個,b有1個;第二位a有2個,b有1個。因為第一位和第二位是對稱的,所以我們要合起來考慮,a一共有4個,b一共有2個,a的數量更多,因此這些位置要替換成a。思路就是這樣,實現的方式有很多。但是不要盲目的memset,因為有可能超時。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的K-Complete Word CodeForces - 1332C(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Exercising Walk Code
- 下一篇: 中国半导体芯片目前的现状