C. K-Complete Word

C. K-Complete Word

time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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
inputCopy
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp
outputCopy
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.

回文串s的周期为T,那么每一个周期一定是回文串。
若干个相同的回文串叠加起来,结果仍然是回文串
s字符串由k个相等的回文组成的回文串

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f
typedef long long ll;
int t;
char s[200001];
int mp[200001][30];
int main()
{
   cin>>t;
   int n,k;
   while(t--)
   {
       cin>>n>>k;
       cin>>s+1;
       for(int i=1;i<=n;i++)
           for(int j=0;j<26;j++)
               mp[i][j]=0;
       int ans=0;
       for(int i=1;i<=(k+1)/2;i++)
       {
          int maxn=0;//s[j+i-1]与s[j+k-i]应相等,求出有最大相等字母个数;       
            for(int j=1;j<=n;j+=k)//j+=k;第j组字符串,共n/k组;
           {
               mp[i][s[j+i-1]-'a']++;
              if(k!=2*i-1)//k为奇数时,中间值无相对应的回文
               {
                mp[i][s[j+k-i]-'a']++;
               }
               maxn=max( mp[i][s[j+i-1]-'a'],max( mp[i][s[j+k-i]-'a'],maxn));
           }
           if(k==2*i-1)
              { ans+=(n/k)-maxn;}
           else
            ans+=2*(n/k)-maxn;//n/k组,每组两个;再减去最大相等的字母个数
       }
       cout<<ans<<endl;
   }
    return 0;
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值