Word $s$ of length $n$ is called $k$-complete if
- $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$;
- $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le 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 \le i \le 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 \le t\le 10^5$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $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 \cdot 10^5$.
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 a aaa aa.
In the second test case, the given word itself is $k$-complete.
Solution:
#include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937 mrand(random_device{}()); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head const int N=201000; int _,n,k,f[N]; char s[N]; map<char,int> hs[N]; int find(int x) { return f[x]==x?x:f[x]=find(f[x]); } int main() { for (scanf("%d",&_);_;_--) { scanf("%d%d",&n,&k); scanf("%s",s); for (int i=0;i<n;i++) f[i]=i; for (int i=0;i<n;i++) f[find(i)]=find(n-1-i); for (int i=0;i<n-k;i++) f[find(i)]=find(i+k); for (int i=0;i<n;i++) hs[i].clear(); for (int i=0;i<n;i++) hs[find(i)][s[i]]++; int ans=0; for (int i=0;i<n;i++) { int s=0,mx=0; for (auto p:hs[i]) s+=p.se,mx=max(mx,p.se); ans+=s-mx; } printf("%d\n",ans); } }