CodeForces 219C Color Stripe

來源:互聯網
上載者:User

標籤:

Color StripeTime Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64uSubmit Status Practice CodeForces 219C

Description

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.

Sample Input

Input
6 3
ABBACC
Output
2
ABCACA
Input
3 2
BBB
Output
1
BAB
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int main()  6 { 7   int n,m; 8   char s[500100]; 9   while(scanf("%d%d",&n,&m)!=EOF)10   {11   scanf("%s",s);12   int i,ans=0;13   m+=64;14   if(n==1)15  {16      cout<<ans<<endl<<s<<endl;17      continue;18  }19      if(m==66)20      {21          int l=0,r=0;22          for(i=0;i<n;i++)23          if(i%2==0)24          {25              if(s[i]==‘B‘) l++;26              else r++;27          }28          else29          {30              if(s[i]==‘A‘) l++;31              else r++;32          }33          if(l<r)34          {35              cout<<l<<endl;36              for(i=0;i<n;i++)37              if(i%2==0) cout<<‘A‘;38              else cout<<‘B‘;39          }40          else41          {42              cout<<r<<endl;43              for(i=0;i<n;i++)44              if(i%2==0) cout<<‘B‘;45              else cout<<‘A‘;46          }47          cout<<endl;48          continue;49      }50   s[n]=‘#‘;51    for(i=1;i<n;i++)52   if(s[i]==s[i-1])53   {54       ans++;55       for(int j=65;j<=m;j++)56       if(j==s[i]||j==s[i+1]) continue;57       else 58       {59           s[i]=j;60           break;61       }62   }63   s[n]=‘\0‘;64   cout<<ans<<endl<<s<<endl;65 66  }67     return 0;68 }
View Code

 

CodeForces 219C Color Stripe

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.