Winter training training game 1.17 C

Title effect
has a length of n (1 <= n <= 2 * 10 ^ 5) are three and only contain the letters "R", "B", "G" string s.
You can modify any of the letters to the other two letters.
For any i, j, if s [i] = s [j ], then the requirement | ij | mod 3 = 0.
The minimum required number of modifications.
Problem-solving ideas
first three letters identified, then the back of the letter also determined.
So long as the three letter sequence beginning determined (total of six), each sequence can be calculated again to the minimum value.
Code

#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	int len;
	string st;
	cin>>len;
	cin>>st;
	int ans=999999999;
	int w=0;
	string s[]={"GRB","GBR","BRG","BGR","RGB","RBG"};
    for (int k=0;k<6;k++)
    {
      int p=0,ti=0;
	  for (int i=0;i<len;i++)
	    {
	    	if (s[k][p]!=st[i]) ti++;
	    	p=(p+1)%3;
	    }
	  if (ti<ans)
	  {
	  	ans=ti;
	  	w=k;
	  }  
	}
	cout<<ans<<endl;
	for (int i=1;i<=len/3;i++)
	  cout<<s[w];
	for (int i=0;i<len%3;i++)
	  cout<<s[w][i];  
}
Published 39 original articles · won praise 0 · Views 556

Guess you like

Origin blog.csdn.net/weixin_45723759/article/details/104064345