Problem solution [P7919 [Kubic] ABC]

Meaning: for a string \ (S \in \{\texttt{A,B,C} \} \) with a length of \ (n \), given an operation, for three characters \ (pA,pB,pC\in\{\texttt{A,B,C} \} \), make all \ (\ texttt{A}\to pA \), all \ (\ texttt{B}\to pB \) and all \ (\ texttt{C}\to pC \) in \ (S \). Find the minimum number of operations that make any two adjacent characters in \ (S \) different and one of the construction schemes.

Obviously, if the interval length is greater than one to make any two adjacent characters in \ (S \) different, there must be \ (pA \not= pB \not= pC \), otherwise there may be more adjacent equal characters in the interval.

It is easy to know that for such an operation, whether the adjacent characters in other positions are equal will not change except the characters at both ends of the interval. So we come to the conclusion that one operation can only make one or two adjacent identical characters no longer the same.

According to the above conclusion, if the number of positions satisfying \ (S_i=S_{i+1} \) in the string is \ (t \), the minimum number of operations is \ (\ left \lceil \dfrac{t}{2} \right \rceil \) times (because if there is one left, you must use one more operation).

Next, consider how to construct the scheme. According to the above conclusion, if the matching \ (S_i=S_{i+1} \) positions are \ (L \) and \ (R \) respectively, in order to make the adjacent characters at both ends different, we need to operate the interval \ ([l+1,r] \) once to make \ (S_{l+1} \) and \ (S_r \) different from \ (S_{l} \) and \ (S_{r+1} \). Then discuss the value of \ (pA,pB,pC \). If \ (S_l \not= S_r \), we just need to turn these two characters into each other (such as \ (\ texttt{A}\to\texttt{B},\texttt{B}\to\texttt{A} \), and output BAC for obvious reasons); If \ (S_l = S_r \), consider rotation (i.e. \ (\ texttt{A}\to\texttt{B},\texttt{B}\to\texttt{C},\texttt{C}\to\texttt{A} \) and output operation BCA) to avoid the possible impact of \ (s {L + 2} \) and \ (s {R-1} \) on the answer.

For the last group left when \ (t \) is odd, consider special treatment. Let the last group be \ (S_k=S_{k+1} \), similarly, we just need to change \ (S_{k+1} \). By discussing the relationship between \ (s {K + 1} \) and \ (s {K + 2} \) and the character situation, we can get the specific transformation mode, as long as the last transformed \ (s {K + 1} \) is not equal to \ (s {K + 2} \).

(of course, according to the official solution, every transformation can meet all the conditions by taking turns, and the last group can avoid the discussion by extending the interval to \ ([k+1,n] \). After all, it wants to be troublesome (SAD))

int n;
char s[N],op2[5]="BCA";
int p[N],cnt;

inline void Solve()
{
	scanf("%d%s",&n,s+1);
	if(n==1){printf("0\n");return;}
	for(int i=1;i<n;i++) if(s[i]==s[i+1]) p[++cnt]=i;
	printf("%d\n",(cnt+1)/2);
	int i;
	for(i=1;i<=cnt-1;i+=2)
	{
		char op[5]="ABC";
		int l=p[i],r=p[i+1];
		if(s[l+1]!=s[r])
		{
			swap(op[s[l+1]-'A'],op[s[r]-'A']);
			printf("%d %d %s\n",l+1,r,op);
		}
		else
		{
			printf("%d %d %s\n",l+1,r,op2);
		}
	}
	if(cnt&1)
	{
		int l=p[cnt];
		char op[5]="ABC";
		if(l==n-1 || s[l+1]==s[l+2])
		{
			printf("%d %d %s\n",l+1,l+1,op2);
		}
		else
		{
			if(s[l+1]=='A'&&s[l+2]=='B') strcpy(op,"CAB");
			if(s[l+1]=='B'&&s[l+2]=='A') strcpy(op,"ACB");
			if(s[l+1]=='A'&&s[l+2]=='C') strcpy(op,"BAC");
			if(s[l+1]=='C'&&s[l+2]=='A') strcpy(op,"ACB");
			if(s[l+1]=='B'&&s[l+2]=='C') strcpy(op,"BAC");
			if(s[l+1]=='C'&&s[l+2]=='B') strcpy(op,"BCA");
			printf("%d %d %s\n",l+1,l+1,op);
		}
	}
	return ;
}
/*
Realization of official problem solving ideas
inline void Solve()
{
	scanf("%d%s",&n,s+1);
	if(n==1){printf("0\n");return;}
	for(int i=1;i<n;i++) if(s[i]==s[i+1]) p[++cnt]=i;
	if(cnt&1) p[++cnt]=n;
	printf("%d\n",cnt/2);
	for(int i=1;i<=cnt;i+=2)
	{
		int l=p[i],r=p[i+1];
		printf("%d %d %s\n",l+1,r,op);
	}
	return ;
}
*/

Posted by ifubad on Tue, 02 Nov 2021 00:09:02 -0700