Problem: 2050E - Three Strings
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 6):
#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
struct node {
	char ch;
	node* next;
};
const int maxsize = 1e3 + 7;
char a[maxsize], b[maxsize], c[maxsize * 2];
int lena, lenb, lenc;
node* s;
int ans;
int check() {
	node* cur = s->next;
	int pos = 0;
	int cnt = 0;
	while (cur != NULL) {
		if (cur->ch != c[pos++]) cnt++;
		cur = cur->next;
	}
	return cnt;
}
void init() {
	s = (node*)malloc(sizeof(node)); 
	s->next = NULL;
	node* pos;
	node* cur = s;
	for (int i = 0; i < lena; i++) {
		pos = (node*)malloc(sizeof(node));
		pos->ch = a[i];
		pos->next = NULL;
		cur->next = pos;
		cur = pos;
	}
}
void dfs(int pos,node* cur) {
	// only can we insert b[pos] into the position which after cur;
	// if all the string is inserted,teh string c has been finished
	if (pos == lenb) {
		int change_count = check();
		if (ans == -1 || ans > change_count) ans = change_count;
		return;
	}
	// only can insert node after point
	if (cur->next == NULL) {
		node* point = (node*)malloc(sizeof(node));
		point->ch = b[pos];
		point->next = NULL;
		cur->next = point;
		dfs(pos + 1, point);
		free(point);
		cur->next = NULL;
	}
	else {
		// do not insert
		dfs(pos, cur->next);
		// insert
		node* point = (node*)malloc(sizeof(node));
		point->ch = b[pos];
		point->next = cur->next;
		cur->next = point;
		dfs(pos + 1, point);
		cur->next = point->next;
		free(point);
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%s%s%s", a, b, c);
		lena = strlen(a); lenb = strlen(b); lenc = strlen(c);
		ans = -1;
		init();
		dfs(0,s);
		printf("%d\n", ans);
	}
	return 0;
}

Submission (verdict: Time limit exceeded on test 6):

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
struct node {
	char ch;
	node* next;
};
const int maxsize = 1e3 + 7;
char a[maxsize], b[maxsize], c[maxsize * 2];
int lena, lenb, lenc;
node* s;
int ans;
int check() {
	node* cur = s->next;
	int pos = 0;
	int cnt = 0;
	while (cur != NULL) {
		if (cur->ch != c[pos++]) cnt++;
		cur = cur->next;
	}
	return cnt;
}
void init() {
	s = (node*)malloc(sizeof(node)); 
	s->next = NULL;
	node* pos;
	node* cur = s;
	for (int i = 0; i < lena; i++) {
		pos = (node*)malloc(sizeof(node));
		pos->ch = a[i];
		pos->next = NULL;
		cur->next = pos;
		cur = pos;
	}
}
void dfs(int pos, node* cur,int c_pos, int error_cnt) {
	// only can we insert b[pos] into the position which after cur;
	// if all the string is inserted,teh string c has been finished
	if (pos == lenb) {
		node* tmp = cur->next;
		while (tmp != NULL) {
			if (tmp->ch != c[c_pos]) error_cnt++;
			tmp = tmp->next; c_pos++;
		}
		if (ans > error_cnt) ans = error_cnt;
		return;
	}
	if (error_cnt >= ans) return;
	// only can insert node after point
	if (cur->next == NULL) {
		node* point = (node*)malloc(sizeof(node));
		point->ch = b[pos];
		point->next = NULL;
		cur->next = point;
		if (b[pos] != c[c_pos]) dfs(pos + 1, point, c_pos + 1, error_cnt + 1);
		else dfs(pos + 1, point, c_pos + 1, error_cnt);
		free(point);
		cur->next = NULL;
	}
	else {
		// do not insert
		if (cur->next->ch != c[c_pos]) dfs(pos, cur->next, c_pos + 1, error_cnt + 1);
		else dfs(pos, cur->next, c_pos + 1, error_cnt);
		// insert
		node* point = (node*)malloc(sizeof(node));
		point->ch = b[pos];
		point->next = cur->next;
		cur->next = point;
		if (b[pos] != c[c_pos]) dfs(pos + 1, point, c_pos + 1, error_cnt + 1);
		else dfs(pos + 1, point, c_pos + 1, error_cnt);
		cur->next = point->next;
		free(point);
	}
}
int main()
{
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%s%s%s", a, b, c);
		lena = strlen(a); lenb = strlen(b); lenc = strlen(c);
		ans = maxsize * 2;
		if (lena < lenb) {
			swap(a, b);
			swap(lena, lenb);
		}
		init();
		dfs(0, s, 0, 0);
		printf("%d\n", ans);
	}
	return 0;
}