codeforces 1332C K-Complete Word

32 篇文章 0 订阅
13 篇文章 0 订阅

K-Complete Word

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Word 𝑠s of length 𝑛n is called 𝑘k-complete if

  • 𝑠s is a palindrome, i.e. 𝑠𝑖=𝑠𝑛+1−𝑖si=sn+1−i for all 1≤𝑖≤𝑛1≤i≤n;
  • 𝑠s has a period of 𝑘k, i.e. 𝑠𝑖=𝑠𝑘+𝑖si=sk+i for all 1≤𝑖≤𝑛−𝑘1≤i≤n−k.

 

For example, "abaaba" is a 33-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≤𝑖≤𝑛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≤𝑡≤1051≤t≤105) — the number of test cases.

The first line of each test case contains two integers 𝑛n and 𝑘k (1≤𝑘<𝑛≤2⋅1051≤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⋅1052⋅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.

题意:给 长度 n 的串 和k

要字符串满足 1.回文 2 s[i+k] = s[i],问需要改变多少个字符

利用并查集将相同位置的合并,然后变成该相同位置最多的字符

#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define f(i,a,b) for(int i = a; i < b; i++)
typedef pair<int,int> P;
#define ll long long
int  f[200010];
vector<int> v[200010];
map<int,int> mp;
int find(int x){
  return x == f[x]?x:f[x] = find(f[x]);
}

void merge(int x, int y)
{
      // cout << x << " :" << y << endl;

    int f1 = find(x);
    int f2 = find(y);
    // cout << f1 << " ::" << f2 << endl;
    if(f1 != f2) f[f1] = f2;
}

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while(t--)
    {
        int n,k;
        cin >> n >> k;

        string s;
        cin >> s;
        ff(i,1,n) f[i] = i,v[i].clear();
        // ff(i,1,n)
        // printf("%d ",find(i) );
        // puts("");
        f(i,0,n)
        {
            merge(i + 1,n - i);
            if(i + k + 1 <= n)
            merge(i + 1,i + k + 1);
        }

        // ff(i,1,n)
        // printf("%d ",find(i) );
        // puts("");
        ff(i,1,n)
          v[find(f[i])].push_back(s[i - 1] - 'a');
        int ans = 0;
        ff(i,1,n)
          if(i == find(f[i]))
          {
              mp.clear();int maxx = 0;
              f(j,0,v[f[i]].size())
              {
                  mp[v[f[i]][j]]++;
                  maxx = max(maxx,mp[v[f[i]][j]]);
              }
              ans+=v[f[i]].size() - maxx;
          }
        cout << ans << endl;
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值