Codeforce219C-Color Stripe

╰+攻爆jí腚メ 2021-11-14 13:34 370阅读 0赞

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

  1. 6 3
  2. ABBACC

output

Copy

  1. 2
  2. ABCACA

input

Copy

  1. 3 2
  2. BBB

output

Copy

  1. 1
  2. BAB
  3. 题意:给你一行n个元素,k种颜色,要求相邻的元素颜色不能相同,输出最少改变颜色的数量和n个元素最后的颜色(若有多种可能可任意输出一种)
  4. 注意:k==2时必定是奇偶位不相同,要选一个最少改变方案,所以要知道奇位放A要改变的数多还是偶位放A要改变的数多,在训练时卡在这种情况了,
  5. 其实一开始想到了ABBA这种情况,但当时的思路是只改变相同颜色的元素,没想到不同颜色元素也能改变,所以当时就假设如果有偶数个相同颜色的元素,其两边元素颜色必不同,结果就一直wrong answer on the test 15...
  6. 1 #include<bits/stdc++.h>
  7. 2 using namespace std;
  8. 3 const int amn=5e5+5;
  9. 4 char mp[amn];
  10. 5 int a[amn],c[30];
  11. 6 int main(){
  12. 7 int n,k;
  13. 8 ios::sync_with_stdio(0);
  14. 9 cin>>n>>k;
  15. 10 for(int i=1;i<=n;i++){
  16. 11 cin>>mp[i];
  17. 12 a[i]=mp[i]-'A'+1;
  18. 13 }
  19. 14 int ans=0,c1=0,c2=0;
  20. 15 if(k==2){ ///k==2时必定是奇偶位不相同,要选一个最少改变方案,所以要知道奇位放A要改变的数多还是偶位放A要改变的数多
  21. 16 for(int i=1;i<=n;i++){ /// 我们先假设奇位为A,偶位为B,因为有可能合法情况是偶位为A,奇数位为B,所以最后要比较哪个不合法的数量少,这样更改少的那个才能得到最少改变方案,故下面统计不合法数,
  22. 17 if(i%2==0){ ///若偶位为A,则A的不合法数加1,否则B的不合法数加1
  23. 18 if(mp[i]=='A')c1++;
  24. 19 else c2++;
  25. 20 }
  26. 21 else{ ///若偶位为A,则B的不合法数加1,否则A的不合法数加1
  27. 22 if(mp[i]=='A')c2++;
  28. 23 else c1++;
  29. 24 }
  30. 25 }
  31. 26 ans=min(c1,c2); ///选一个最小不合法数,得到最少改变方案
  32. 27 for(int i=1;i<=n;i++){
  33. 28 if(ans==c1){ ///若偶数位为A是不合法的,要将奇位变为A,偶位变为B
  34. 29 if(i%2)
  35. 30 mp[i]='A';
  36. 31 else
  37. 32 mp[i]='B';
  38. 33 }
  39. 34 else{
  40. 35 if(i%2) ///若奇数为位A是不合法的,要将偶数位变为A,奇数位变为B
  41. 36 mp[i]='B';
  42. 37 else
  43. 38 mp[i]='A';
  44. 39 }
  45. 40 }
  46. 41 }
  47. 42 else{
  48. 43 a[n+1]=27;
  49. 44 memset(c,0,sizeof c);
  50. 45 bool f=0,d=0;
  51. 46 int fr=1,ta=0,frc,mic,tac,it,cc;
  52. 47 for(int i=2;i<=n;i++){
  53. 48 if(a[i]==a[i-1]){
  54. 49 d=1;
  55. 50 if(fr>ta){
  56. 51 fr=i-1;
  57. 52 if(i-2>=1){
  58. 53 frc=a[fr-1];//cout<<frc<<'!'<<endl;
  59. 54 c[a[i]]=c[frc]=1;
  60. 55 }
  61. 56 else{
  62. 57 c[a[i]]=1;
  63. 58 f=1;
  64. 59 }
  65. 60 mic=a[i];
  66. 61 ta=i+1;
  67. 62 tac=a[ta];
  68. 63 c[tac]=1;
  69. 64 }
  70. 65 else{
  71. 66
  72. 67 ta=i+1;
  73. 68 tac=a[ta];
  74. 69 c[tac]=1;
  75. 70 }
  76. 71 //printf("i:%d fr:%d ta:%d\n",i,fr,ta);
  77. 72 if(!f&&i==n){
  78. 73 ans+=(ta-fr)/2;
  79. 74 it=fr+1;
  80. 75 cc=frc;
  81. 76 while(it<ta){
  82. 77 a[it]=cc;
  83. 78 mp[it]=a[it]-1+'A';
  84. 79 it+=2;
  85. 80 }
  86. 81 break;
  87. 82 }
  88. 83
  89. 84 }
  90. 85 else if(d){
  91. 86 //printf("i:%d fr:%d ta:%d tac:%d\n",i,fr,ta,tac);
  92. 87 d=0;
  93. 88 ans+=(ta-fr)/2;
  94. 89 if(f){
  95. 90 cc=tac;
  96. 91 if(ta>n)
  97. 92 for(int i=1;i<=k;i++){
  98. 93 if(!c[i]){
  99. 94 cc=i;
  100. 95 break;
  101. 96 }
  102. 97 }
  103. 98 it=ta-2;
  104. 99 while(it>0){
  105. 100 a[it]=cc;
  106. 101 mp[it]=a[it]-1+'A';
  107. 102 it-=2;
  108. 103 }
  109. 104 f=0;
  110. 105 }
  111. 106 else{
  112. 107 cc=frc;
  113. 108 //printf("---\ni:%d frc: %d tac: %d\n",i,frc,tac);
  114. 109 //for(int i=1;i<=26;i++)cout<<c[i]<<' ';
  115. 110 //cout<<"\n---\n";
  116. 111 if(frc==tac){
  117. 112 for(int i=1;i<=k;i++){
  118. 113 if(!c[i]){
  119. 114 cc=i;
  120. 115 break;
  121. 116 }
  122. 117 }
  123. 118 }
  124. 119 //cout<<i<<'?'<<cc<<endl;
  125. 120 memset(c,0,sizeof c);
  126. 121 it=fr+1;
  127. 122 while(it<ta){
  128. 123 a[it]=cc;
  129. 124 mp[it]=a[it]-1+'A';
  130. 125 it+=2;
  131. 126 }
  132. 127 }
  133. 128 fr=ta;
  134. 129 ta--;
  135. 130 }
  136. 131 }
  137. 132 if(f){
  138. 133 ans+=(ta-fr)/2;
  139. 134 for(int i=1;i<=k;i++){
  140. 135 if(!c[i]){
  141. 136 cc=i;
  142. 137 break;
  143. 138 }
  144. 139 }
  145. 140 memset(c,0,sizeof c);
  146. 141 it=fr+1;
  147. 142 while(it<ta){
  148. 143 a[it]=cc;
  149. 144 mp[it]=a[it]-1+'A';
  150. 145 it+=2;
  151. 146 }
  152. 147 fr=ta;
  153. 148 ta--;
  154. 149 }
  155. 150 }
  156. 151 cout<<ans<<endl;
  157. 152 cout<<mp+1<<endl;
  158. 153 }

转载于:https://www.cnblogs.com/brainm/p/11270244.html

发表评论

表情:
评论列表 (有 0 条评论,370人围观)

还没有评论,来说两句吧...

相关阅读