It may be the same number of chunks in the first array gp
For example RRGGGRBBBBRR
Then gp [1 ~ 5] = {2,3,1,4,2}
First of all know, to make the same if not adjacent, only the number of each gp [i] / 2 rounded down to obtain a minimum of change
For example RGGGR, look G, G only need to change the center
For example RGGGGR, look G, may choose to alter or G 1 3 2 4 and the position and,
Finally, consider the impact of end to end to form a ring of the answer
For example RRRGRRR
gp[1~3]={3,1,3}
The above statement can be obtained by the answer is 3/2 + 1/2 + 3/2 = 1 + 0 + 1 = 2
But actually connected end to end 6 sitting together R
At least three people need to change to meet the meaning of the questions
Also, if all of the same color
For example RRRRR
The above statement is only needed to change the 5/2 = 2
I.e., change the position of the person 2 and 4
But this will result in the 1 and 5 are connected end to end with the same color sat
Laid answer judged as required (5 + 1) in this case / 2 = 3
1 /* 2 Written By StelaYuri 3 */ 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 int main(){ 8 ios::sync_with_stdio(0);cin.tie(0); 9 int T,t,N,m,i,gp[110],ans; 10 string s; 11 cin>>T; 12 for(t=1;t<=T;t++){ 13 cin>>N>>s; 14 s=" "+s;//下标移位 15 ans=m=0; 16 for(i=1;i<=N;i++) 17 if(s[i]==s[i-1]) 18 gp[m]++; 19 else 20 gp[++m]=1; 21 if(s[1]==s[N]&&N!=1) 22 if(m!=1){ 23 gp[1]+=gp[m]; 24 m--; 25 } 26 else 27 gp[1]++; 28 for(i=1;i<=m;i++) 29 ans+=gp[i]/2; 30 cout<<"Case #"<<t<<":\n"<<ans<<'\n'; 31 } 32 33 return 0; 34 }