Codeforce219C-Color Stripe

E. Color Stripe
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A colored stripe is represented by a horizontal row of n square cells, each cell is pained one of k colors. Your task is to repaint the minimum number of cells so that no two neighbouring cells are of the same color. You can use any color from 1 to k to repaint the cells.

Input

The first input line contains two integers n and k (1 ≤ n ≤ 5·105; 2 ≤ k ≤ 26). The second line contains n uppercase English letters. Letter "A" stands for the first color, letter "B" stands for the second color and so on. The first k English letters may be used. Each letter represents the color of the corresponding cell of the stripe.

Output

Print a single integer — the required minimum number of repaintings. In the second line print any possible variant of the repainted stripe.

Examples
input
Copy
3 6 
ABBACC
output
Copy
2
ABCACA
input
Copy
2 3 a 
BBB
output
Copy
. 1 
BAB-

question is intended: to give you a row of n elements, k colors, requires not the same color adjacent element, changing the final color output least n elements and the number of colors (if any output may be more likely one)
Note: k == 2 parity bits are not necessarily the same, to choose the least change a program, so you know you want to change the odd bit put a number of more or even bit a place to change a few more, this card during training case, and
in fact, the beginning thought ABBA this case, but it was the only idea is to change the elements of the same color, I did not expect a different color elements also change, so it was assumed that if there are an even number of elements of the same color, its sides element color will be different, the results have been wrong answer on the test 15 ...
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int amn=5e5+5;
  4 char mp[amn];
  5 int a[amn],c[30];
  6 int main(){
  7     int n,k;
  8     ios::sync_with_stdio(0);
  9     cin>>n>>k;
 10     for(int i=1;i<=n;i++){
 11         cin>>mp[i];
 12         A [I] MP = [I] - ' A ' + . 1 ;
 13 is      }
 14      int ANS = 0 , C1 = 0 , C2 = 0 ;
 15      IF (== K 2 ) {        /// time must k == 2 is a parity bit is not the same, to choose a minimum change program, so to know the odd bits discharge are more or even bit a to change the discharge for multiple a to change 
16          for ( int I = . 1 ; I <= n-; I ++) {   /// let's assume that the odd bits of a, even bit is B, because there may be a legitimate case even bit as a, odd bits B, so in the end you want to compare what is not legitimate small number, so that changes can be small to give the least change scheme, it is not statistically valid number below, 
. 17              IF (I% 2 == 0 ) {      /// If the a bit is even, the number of illegal a plus 1, or invalid number B plus 1
18 is                  IF (MP [I] == ' A ' ) C1 ++ ;
 . 19                  the else C2 ++ ;
 20 is              }
 21 is              the else {            /// If the A bit is even, the number of illegal B plus 1, or invalid number of A plus 1 
22 is                  IF (MP [I] == ' a ' ) C2 ++ ;
 23 is                  the else C1 ++ ;
 24              }
 25          }
 26 is          ANS = min (C1, C2); /// selected from a number of illegal minimum, the least change scheme to obtain 
27          for ( int I = . 1 ; I <= n-; I ++ ) {
28              IF (C1 == ANS) {     /// If A is even bit is not legal, to the odd bit is set to A, becomes even bit B 
29                  IF (% I 2 ) 
 30                      MP [I] = ' A ' ;
 31 is                  the else 
32                      MP [I] = ' B ' ;
 33 is              }
 34 is              the else {
 35                  IF (% I 2 )      /// If the a bit is an odd number not legal, to a becomes even bit, odd bit variant is B 
36                      MP [I] = ' B ' ;
 37 [                  the else 
38 is                     mp[i]='A';
 39             }
 40         }
 41     }
 42     else{
 43     a[n+1]=27;
 44     memset(c,0,sizeof c);
 45     bool f=0,d=0;
 46     int fr=1,ta=0,frc,mic,tac,it,cc;
 47     for(int i=2;i<=n;i++){
 48         if(a[i]==a[i-1]){
 49             d=1;
 50             if(fr>ta){
 51                 fr=i-1;
 52                 if(i-2>=1){
 53                     frc=a[fr-1];//cout<<frc<<'!'<<endl;
 54                     c[a[i]]=c[frc]=1;
 55                 }
 56                 else{
 57                     c[a[i]]=1;
 58                     f=1;
 59                 }
 60                 mic=a[i];
 61                 ta=i+1;
 62                 tac=a[ta];
 63                 c[tac]=1;
 64             }
 65             else{
 66 
 67                 ta=i+1;
 68                 tac=a[ta];
 69                 c[tac]=1;
 70             }
 71                 //printf("i:%d fr:%d ta:%d\n",i,fr,ta);
 72                 if(!f&&i==n){
 73                     ans+=(ta-fr)/2;
 74                     it=fr+1;
 75                     cc=frc;
 76                     while(it<ta){
 77                         a[it]=cc;
 78                         mp[it]=a[it]-1+'A';
 79                         it+=2;
 80                     }
 81                     break;
 82                 }
 83 
 84         }
 85         else if(d){
 86             //printf("i:%d fr:%d ta:%d tac:%d\n",i,fr,ta,tac);
 87             d=0;
 88             ans+=(ta-fr)/2;
 89             if(f){
 90                 cc=tac;
 91                 if(ta>n)
 92                     for(int i=1;i<=k;i++){
 93                         if(!c[i]){
 94                             cc=i;
 95                             break;
 96                         }
 97                     }
 98                 it=ta-2;
 99                 while(it>0){
100                     a[it]=cc;
101                     mp[it]=a[it]-1+'A';
102                     it-=2;
103                 }
104                 f=0;
105             }
106             else{
107                 cc=frc;
108                 //printf("---\ni:%d frc: %d tac: %d\n",i,frc,tac);
109                 //for(int i=1;i<=26;i++)cout<<c[i]<<' ';
110                 //cout<<"\n---\n";
111                 if(frc==tac){
112                     for(int i=1;i<=k;i++){
113                         if(!c[i]){
114                             cc=i;
115                             break;
116                         }
117                     }
118                 }
119                 //cout<<i<<'?'<<cc<<endl;
120                 memset(c,0,sizeof c);
121                     it=fr+1;
122                     while(it<ta){
123                         a[it]=cc;
124                         mp[it]=a[it]-1+'A';
125                         it+=2;
126                     }
127             }
128             fr=ta;
129             ta--;
130         }
131     }
132     if(f){
133         ans+=(ta-fr)/2;
134         for(int i=1;i<=k;i++){
135                         if(!c[i]){
136                             cc=i;
137                             break;
138             }
139         }
140         memset(c,0,sizeof c);
141                     it=fr+1;
142                     while(it<ta){
143                         a[it]=cc;
144                         mp[it]=a[it]-1+'A';
145                         it+=2;
146                     }
147             fr=ta;
148             ta--;
149     }
150     }
151     cout<<ans<<endl;
152     cout<<mp+1<<endl;
153 }

 

 

Guess you like

Origin www.cnblogs.com/brainm/p/11270244.html