CodeForces 1332-C K-Complete Word (palindromic sequence segment)

 

 

 

 

 

 

 

https://codeforces.com/contest/1332/problem/C

 

 

 

 

 

Question is intended: to a string of length n, so that you will modify it into equal n / k palindromic segment section, find the minimum number of modifications.

Ideas: we first string length by k Subdivision:

{XXX...XX}{XXX...XX}...{XXX...XX}

The last requirement is a palindromic sequence, so that each partition are exactly the same palindrome sequence, record length [k / 2] number of each letter appears in each position can be a partition, and greedy, most often occurs the letters do not change, the whole get rid of other letters.

 

 

 1 #include <bits/stdc++.h>
 2 typedef long long LL;
 3 const int INF=0x3f3f3f3f;
 4 const double eps =1e-8;
 5 const int mod=1e8;
 6 const int maxn=2e5+10;
 7 using namespace std;
 8  
 9 int a[maxn][30];
10 char str[maxn];
11  
12 int main()
13 {
14     #ifdef DEBUG
15     freopen("sample.txt","r",stdin);
16     #endif
17     
18     int T;
19     scanf("%d",&T);
20     while(T--)
21     {
22         int n,k;
23         scanf("%d %d %s",&n,&k,str);
24         for(int i=0;i<n;i++)
25             a[min(i%k,k-i%k-1)][str[i]-'a']++;
26         int ans=0;
27         for(int i=0;i<k;i++)
28         {
29             int num=0;
30             int MAX=0;
31             for(int j=0;j<26;j++)
32             {
33                 num+=a[i][j];
34                 MAX=max(MAX,a[i][j]);
35                 a[i][j]=0;
36             }
37             ans+=num-MAX;
38         }
39         printf("%d\n",ans);
40     }
41     
42     return 0;
43 }

 

 

 

 

 

 

 

 

-

Guess you like

Origin www.cnblogs.com/jiamian/p/12616178.html