日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

K-Complete Word CodeForces - 1332C(贪心)

發(fā)布時(shí)間:2023/12/15 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 K-Complete Word CodeForces - 1332C(贪心) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.
唉,挺簡單的一道題目給整復(fù)雜了。。
思路:根據(jù)題目要求,我們可以推斷出,給定的字符串,每k位就是一個(gè)回文字符串。這樣的話,我們就考慮這k位的字符串。對于這k位的回文字符串,我們要考慮對稱的位置字符的數(shù)量,例如第一個(gè)樣例來說,abaaba,第一位a有2個(gè),b有1個(gè);第二位a有2個(gè),b有1個(gè)。因?yàn)榈谝晃缓偷诙皇菍ΨQ的,所以我們要合起來考慮,a一共有4個(gè),b一共有2個(gè),a的數(shù)量更多,因此這些位置要替換成a。思路就是這樣,實(shí)現(xiàn)的方式有很多。但是不要盲目的memset,因?yàn)橛锌赡艹瑫r(shí)。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=2e5+100; int cnt[maxx][27]; string s; int n,k;inline void init() {for(int i=0;i<k;i++) memset(cnt[i],0,sizeof(cnt[i])); } inline void dfs(int u,int flag) {if(u>=n) return ;cnt[flag][s[u]-'a']++;dfs(u+k,flag); } int main() {int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&k);cin>>s;init();for(int i=0;i<k;i++) dfs(i,i);int l=0,r=k-1;int ans=0,_max=0;while(l<=r)//這樣寫就不用把奇數(shù)的情況單獨(dú)拿出來考慮了。{if(l==r){_max=0;for(int i=0;i<26;i++) _max=max(_max,cnt[l][i]);ans+=_max;break;}else{_max=0;for(int i=0;i<26;i++) _max=max(_max,cnt[l][i]+cnt[r][i]);ans+=_max;l++,r--;}}cout<<n-ans<<endl;}return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的K-Complete Word CodeForces - 1332C(贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲一级二级片 | 亚洲欧洲日产av | 好色先生视频污 | 黄网址在线 | 亚洲国产高清在线 | 色老汉视频| 国产免费一区二区三区最新6 | 亚洲精品一线 | 国产伦理一区 | 日本极品丰满ⅹxxxhd | 一区二区三区激情 | 免费一级片在线观看 | 欧美黑人激情 | 中文字幕第35页 | 伊人久久久久噜噜噜亚洲熟女综合 | 国产999视频 | 日韩精品久久久久久 | 欧美人妻精品一区二区三区 | 在线国产毛片 | 6080福利 | 山村大伦淫第1部分阅读小说 | 久草福利资源在线观看 | 日本成人免费视频 | 成人3d动漫在线观看 | 欧美视频黄色 | 日本免费网 | 在线观看日韩一区二区 | 99久久久精品免费观看国产 | 亚洲一区中文字幕在线 | 国产一级一区二区 | 五月婷婷综合网 | 久久这里只有精品23 | 国产一区精品久久 | 少妇精品一区二区三区 | 中文字幕在线播出 | 亚洲激情图片 | 日本福利小视频 | 欧美午夜网站 | 欧美成人免费高清视频 | 日韩一区二区在线免费观看 | 午夜精品久久久久久久久久久久久蜜桃 | 欧美日韩一区二区在线播放 | 久久艹精品视频 | 国产精品999 | 亚洲AV无码精品色毛片浪潮 | 欧美成人精品一区二区免费看片 | 久久午夜网 | 黄色在线免费观看网站 | 国产精品久久免费 | 色老大网站| 91插插插视频 | 男人日女人的网站 | 午夜久久一区 | 亚洲午夜精品久久久久久app | 日本少妇激情舌吻 | 写真福利片hd在线播放 | 西西人体做爰大胆gogo直播 | 欧美韩日精品 | 欧美福利网址 | 欧美成人精品欧美一级私黄 | 成人午夜激情 | 女生扒开尿口 | 大咪咪av| 黄色av网站在线 | 你懂的成人 | 大陆一级黄色片 | 99久久精品免费看国产免费软件 | 欧美日韩综合在线观看 | av在线视屏| 国产91色 | 欧美激情视频网址 | 嫩草天堂| 俄罗斯毛片基地 | 色网站女女 | 中文字幕国产在线 | 国产成人亚洲综合 | 国产精品夜夜夜爽阿娇 | 亚洲视频在线观看一区二区三区 | 男人天堂资源网 | 久久人体视频 | 国产精品jizz视频 | 欧美黄色性生活 | 黄色福利网 | 91在线观看成人 | 日本爽爽爽爽爽爽在线观看免 | 日本真人做爰免费视频120秒 | 国产乱人伦精品 | 亚洲精品视频在线免费 | 九九热在线精品视频 | 午夜伦理影院 | 都市激情自拍偷拍 | 91大奶| 国产视频一区在线 | 久久三级精品 | proumb性欧美在线观看 | 99热国产在线 | 亚洲色图图| 国精产品一区一区三区在线 | 亚洲视频黄色 |