Advertisement
tumaryui

Untitled

Mar 31st, 2020
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. #define all(s) s.begin(),s.end()
  4. using namespace std;
  5.  
  6. main() {
  7. int t;
  8. cin >> t;
  9. while(t--) {
  10. int n, k;
  11. cin >> n >> k;
  12. string s;
  13. cin >> s;
  14. if(n == k) {
  15. int ans = 0;
  16. for(int i = 0; i < n / 2; i++) {
  17. ans += (s[i] != s[n - i - 1]);
  18. }
  19. cout << ans << endl;
  20. continue;
  21. }
  22. int a[k][26], ans = 0;
  23. memset(a, 0, sizeof a);
  24. //n / k % 2 == 0
  25. if(1) {
  26. for(int i = 0; i < n / 2; i++) {
  27. a[i % k][s[i] - 'a']++;
  28. }
  29. reverse(all(s));
  30. for(int i = 0; i < n / 2; i++) {
  31. a[i % k][s[i] - 'a']++;
  32. }
  33. if((n & 1) && (k & 1)) {
  34. a[k / 2][s[n / 2] - 'a']++;
  35. }
  36. for(int i = 0; i < k / 2; i++) {
  37. int mx = 0;
  38. int al = 2 * n / k;
  39. for(int j = 0; j < 26; j++) {
  40. mx = max(mx, a[i][j] + a[k - i - 1][j]);
  41. }
  42. ans += (al - mx);
  43. }
  44. if(k & 1) {
  45. int al = n / k;
  46. int mx = 0;
  47. for(int i = 0; i < 26; i++) {
  48. mx = max(mx, a[k / 2][i]);
  49. }
  50. ans += (al - mx);
  51. }
  52. }
  53. cout << ans << endl;
  54. }
  55. }
  56.  
  57. /*
  58. 0 1 2 3
  59. a
  60. 3
  61.  
  62. //
  63. */
  64.  
  65. // 2 3 5 7 11 13 17 19 23 29 31 37
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement