Language: C++

#include <iostream> 
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>
#include <queue>
#include <stack>
#include <unordered_set>
#include <set>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <cctype>
#include <climits>

using namespace std;

using ii = pair<int, int>;
using vi = vector<int>;
using vii = vector<ii>;
using vvi = vector<vi>;

using ll = long long int;

#define mp(a,b) make_pair(a,b)

const int MOD = 1000 * 1000 * 1000 + 7;

string t, p; vi prep; // n=t.size, m=p.size

void kmpPreprocess() {
	int n = t.size();
	int m = p.size();
	prep.resize(n + m + 10);
	int i = 0, j = -1;
	prep[0] = -1;
	while (i < m) {
		while (j >= 0 && (p[i] != p[j] && p[i] != '?' && p[j] != '?')) 
			j = prep[j];
		
		prep[++i]=++j;
	}
}


int kmpSearch() {
	int n = t.size();
	int m = p.size();
	int i = 0, j = 0;
	vi sol;
	while (i<n) {
		while (j >= 0 && (t[i] != p[j] && t[i] != '?' && p[j] != '?') )
			j = prep[j];
		i++;
		j++;
		//if (i + j == m)
		//	sol.push_back(i-j), j = prep[j];
	}
	return j;
	//return sol;
}

pair<bool,int> kmpContenido() {
	int n = t.size();
	int m = p.size();
	int i = 0, j = 0;
	vi sol;
	while (i < n) {
		while (j >= 0 && (t[i] != p[j] && t[i] != '?' && p[j] != '?'))
			j = prep[j];
		i++;
		j++;
		if (j == m)
			return mp(true, i-j);
	}
	return mp(false,0);
}


int minletras(string & a, string & b, string & c) {
	int sol = 0;
	pair<bool,int> cp1;
	
	t = a;
	p = b;

	kmpPreprocess();
	cp1 = kmpContenido();
	
	if (cp1.first) {
		sol += a.size();
		for (int i = cp1.second; i < cp1.second + b.size(); ++i) {
			a[i] = b[i - cp1.second];
		}
	}
	else {
		int solo = kmpSearch();
		sol += a.size() + b.size() - solo;
		for (int i = 0; i < solo; ++i) {
			a[a.size() - solo + i] = b[i];
		}
	}
	
	if (cp1.first) {
		t = a;
		p = c;
		kmpPreprocess();
		if (kmpContenido().first) {}
		else sol += c.size() - kmpSearch();
	}
	else {
		t = a;
		p = c;
		kmpPreprocess();
		bool a1 = kmpContenido().first;

		t = b;
		p = c;
		kmpPreprocess();
		bool a2 = kmpContenido().first;

		if (a1 || a2) {}
		else sol += c.size() - kmpSearch();
	}
	

	return sol;
}

bool res() {
	string a1, b1, c1;
	cin >> a1 >> b1 >> c1;
	if (!cin) return false;

	string a = a1, b = b1, c = c1;

	int sol = 1000000000;
	sol = min(sol, minletras(a1, b, c));
	string a = a1, b = b1, c = c1;
	sol = min(sol, minletras(a, c, b));
	string a = a1, b = b1, c = c1;
	sol = min(sol, minletras(b, a, c));
	string a = a1, b = b1, c = c1;
	sol = min(sol, minletras(b, c, a));
	string a = a1, b = b1, c = c1;
	sol = min(sol, minletras(c, b, a));
	string a = a1, b = b1, c = c1;
	sol = min(sol, minletras(c, a, b));

	cout << sol << '\n';
	return true;
}

int main() {
	while (res()) {}
	return 0;
}

Compiler output:
a.cc: In function 'bool res()':
a.cc:140:16: error: redeclaration of 'std::string a'
  140 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:136:16: note: 'std::string a' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:140:24: error: redeclaration of 'std::string b'
  140 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:136:24: note: 'std::string b' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:140:32: error: redeclaration of 'std::string c'
  140 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:136:32: note: 'std::string c' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:142:16: error: redeclaration of 'std::string a'
  142 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:136:16: note: 'std::string a' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:142:24: error: redeclaration of 'std::string b'
  142 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:136:24: note: 'std::string b' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:142:32: error: redeclaration of 'std::string c'
  142 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:136:32: note: 'std::string c' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:144:16: error: redeclaration of 'std::string a'
  144 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:136:16: note: 'std::string a' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:144:24: error: redeclaration of 'std::string b'
  144 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:136:24: note: 'std::string b' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:144:32: error: redeclaration of 'std::string c'
  144 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:136:32: note: 'std::string c' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:146:16: error: redeclaration of 'std::string a'
  146 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:136:16: note: 'std::string a' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:146:24: error: redeclaration of 'std::string b'
  146 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:136:24: note: 'std::string b' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:146:32: error: redeclaration of 'std::string c'
  146 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:136:32: note: 'std::string c' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:148:16: error: redeclaration of 'std::string a'
  148 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:136:16: note: 'std::string a' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                ^
a.cc:148:24: error: redeclaration of 'std::string b'
  148 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:136:24: note: 'std::string b' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                        ^
a.cc:148:32: error: redeclaration of 'std::string c'
  148 |         string a = a1, b = b1, c = c1;
      |                                ^
a.cc:136:32: note: 'std::string c' previously declared here
  136 |         string a = a1, b = b1, c = c1;
      |                                ^