Each character in the S1, S2, and S3 will be an uppercase English alphabet letter.
To make AAABCB, you have to take at least four letters from the first material. So this can't be created alchemical.
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<cstring>
int backtrack(char S3[],int n, int N);
struct Point{
char value;
int flag;
}point[200];
int Sum = 0;
int index[100], k = 0, Y_num = 0;
int main(){
char a[201], b[100],S3[100];
gets(a);
gets(b);
gets(S3);
strcat(a, b);
int N = strlen(b)/2;
int i, j;
for (i = 0; i < 4 * N; i++){
point[i].value = a[i];
point[i].flag = 0;
}
backtrack(S3,0,N);
if (Y_num != 0)
printf("YES\n");
else printf("NO\n");
system("pause");
return 0;
}
int backtrack(char S3[], int n, int N){
int i, j = n;
int left = 0, right = 0;
if (n >= 2 * N){
Sum++;
for (k = 0;k<2*N; k++)
//printf("index[k]=%d ", index[k]);
//printf("\n");
//printf("Sum = %d\n", Sum);
if (Sum != 0){
for (i = 0; i < 2 * N; i++){
if (index[i] >= 0 && index[i] < 2 * N) left++;
else right++;
}
//printf("right=%d,left=%d\n", right, left);
if (left == right)
Y_num++;
//printf("YES\n");
//else printf("NO\n");
}
//else printf("NO\n");
return Y_num;
}
for (i = 0; i < 4 * N; i++){
index[n] = i;
if (point[i].flag == 0 && point[i].value == S3[j]){
point[i].flag = 1;
//printf("index[k]=%d\n", index[k]);
backtrack(S3, n+1, N);
point[i].flag = 0;
}
}
}