#include <iostream>

#include <string>

#include <vector>

using namespace std;

int main()

{

    int t;

    cin >> t;

    for (; t > 0; --t)

    {

        string a, b, c;

        cin >> a >> b >> c;

        vector<vector<int>> ans(a.size() + 1, vector<int>(b.size() + 1, 0));

        for (int i = 1; i <= a.size(); ++i)

        {

            if (c[i - 1] != a[i - 1])

                ans[i][0] = ans[i - 1][0] + 1;

        }

        for (int i = 1; i <= b.size(); ++i)

        {

            if (c[i - 1] != b[i - 1])

                ans[0][i] = ans[0][i - 1] + 1;

        }

        for (int i = 1; i <= a.size(); ++i)

        {

            for (int j = 1; j <= b.size(); ++j)

            {

                if (a[i - 1] == c[i + j - 1])

                    ans[i][j] = ans[i - 1][j];

                else

                    ans[i][j] = ans[i - 1][j] + 1;

                if (b[j - 1] == c[i + j - 1])

                    ans[i][j] = min(ans[i][j], ans[i][j - 1]);

                else

                    ans[i][j] = min(ans[i][j], ans[i][j - 1] + 1);

            }

        }

        cout << ans[a.size()][b.size()];

    }

}

#include <iostream>

#include <string>

#include <vector>

using namespace std;

int main()

{

    int t;

    cin >> t;

    for (; t > 0; --t)

    {

        string a, b, c;

        cin >> a >> b >> c;

        vector<vector<int>> ans(a.size() + 1, vector<int>(b.size() + 1, 0));

        for (int i = 1; i <= a.size(); ++i)

        {

            if (c[i - 1] != a[i - 1])

                ans[i][0] = ans[i - 1][0] + 1;

            else

                ans[i][0] = ans[i - 1][0];

        }

        for (int i = 1; i <= b.size(); ++i)

        {

            if (c[i - 1] != b[i - 1])

                ans[0][i] = ans[0][i - 1] + 1;

            else

                ans[0][i] = ans[0][i - 1];

        }

        for (int i = 1; i <= a.size(); ++i)

        {

            for (int j = 1; j <= b.size(); ++j)

            {

                if (a[i - 1] == c[i + j - 1])

                    ans[i][j] = ans[i - 1][j];

                else

                    ans[i][j] = ans[i - 1][j] + 1;

                if (b[j - 1] == c[i + j - 1])

                    ans[i][j] = min(ans[i][j], ans[i][j - 1]);

                else

                    ans[i][j] = min(ans[i][j], ans[i][j - 1] + 1);

            }

        }

        cout << ans[a.size()][b.size()] << endl;

    }

}

Answer: 2


#include <iostream>
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define INF 1000000000

using namespace std;

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    for (int q = 0; q < t; q++) {
        string a, b, c;
        cin >> a >> b >> c;
        int n = a.size();
        int m = b.size();
        a = "`" + a;
        b = "`" + b;
        vector<vector<int>> dp(n + 2, vector<int>(m + 2, INF));
        // for (int i = 0; i < n + 2; i++) {
        //     dp[i][0] = 0;
        // }
        // for (int i = 0; i < m + 2; i++) {
        //     dp[0][i] = 0;
        // }
        dp[1][1] = 0;
        int k = 0;
        while (k < n + m) {
            k++;
            for (int i = 0; i < min(n, k) + 1; i++) {
                int j = k - i;
                if (j < 0 || j > m) continue;
                dp[i + 1][j + 1] = min(dp[i][j + 1] + (c[k - 1] != a[i]), dp[i + 1][j] + (c[k - 1] != b[j]));
            }
        }
        // for (int i = 0; i < n + 2; i++) {
        //     for (int j = 0; j < m + 2; j++) {
        //         cout << dp[i][j] << " ";
        //     }
        //     cout << "\n";
        // }
        // cout << "\n";
        cout << dp[n + 1][m + 1] << "\n";
    }
    return 0;
}

#include <iostream>
#include <bits/stdc++.h>

#define ll long long
#define ld long double
#define INF 1000000000

using namespace std;

int main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    int t;
    cin >> t;
    for (int q = 0; q < t; q++) {
        string a, b, c;
        cin >> a >> b >> c;
        int n = a.size();
        int m = b.size();
        a = "`" + a;
        b = "`" + b;
        vector<vector<int>> dp(n + 2, vector<int>(m + 2, INF));
        // for (int i = 0; i < n + 2; i++) {
        //     dp[i][0] = 0;
        // }
        // for (int i = 0; i < m + 2; i++) {
        //     dp[0][i] = 0;
        // }
        dp[1][1] = 0;
        int k = 0;
        while (k < n + m) {
            k++;
            for (int i = 0; i < min(n, k) + 1; i++) {
                int j = k - i;
                if (j < 0 || j > m) continue;
                dp[i + 1][j + 1] = min(dp[i][j + 1] + (c[k - 1] != a[i]), dp[i + 1][j] + (c[k - 1] != b[j]));
            }
        }
        for (int i = 0; i < n + 2; i++) {
            for (int j = 0; j < m + 2; j++) {
                cout << dp[i][j] << " ";
            }
            cout << "\n";
        }
        cout << "\n";
        cout << dp[n + 1][m + 1] << "\n";
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

long long t,dp[1005][1005];

string a,b,c;

void solve(){

    cin>>a>>b>>c;

    vector<pair<int,int>> v;

    int n=a.size(),m=b.size(),k=c.size();

    a=' '+a; b=' '+b; c=' '+c;

    for(int i=1; i<=n; i++){

        dp[i][0]=dp[i-1][0]+(a[i]!=c[i]);

        v.push_back({i,0});

    }

    for(int i=1; i<=m; i++){

        dp[0][i]=dp[0][i-1]+(b[i]!=c[i]);

        v.push_back({0,i});

    }

    for(int i=1; i<=n; i++){

        for(int j=1; j<=m; j++){

            dp[i][j]=min(dp[i-1][j]+(a[i]!=c[i+j]), dp[i][j-1]+(b[j]!=c[i+j]));

            v.push_back({i,j}); v.push_back({j,i});

        }

    }

    cout<<dp[n][m]<<'\n';

    for(auto x:v){

        dp[x.first][x.second]=0;

    }

}

int main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0); cout.tie(0);

    // freopen(".inp", "r", stdin);

    // freopen(".out", "w", stdout);

    cin>>t;

    while(t--){

        solve();

    }

}

Answer: 1


#include <cstdint>
#include <string>
#include <algorithm>
#include <memory>
#include <vector>
#include <iostream>
#include <functional>
#include <set>
#include <deque>
#include <chrono>
#include <ranges>
#include <random>
#include <numeric>
#include <map>
#include <cassert>
#include <queue>
#include <tuple>
#include <sstream>
#include <fstream>
#include <coroutine>
#include <array>
#include <bitset>
#include <forward_list>
#include <bit>
#include <stack>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
long TEST = 0;
long TEST_TOTAL = 0;
namespace _IOBase {
	int hook_test = 0;
	int min_tot = 0;
	std::stringstream hook_str;
	template<class T> T read() {
		T val;
		std::cin >> val;
		if (hook_test) {
			if (TEST == hook_test&&TEST_TOTAL >= std::max(min_tot, hook_test)) {
				hook_str << val;
				hook_str << " ";
			}
		}
		return val;
	}
	template<class T> void put(const T& v) {
		if (hook_test) {
			if (TEST_TOTAL >= std::max(min_tot, hook_test)) {
				if (TEST == hook_test) {
					std::cout << hook_str.str();
					hook_str = std::stringstream();
				}
				return;
			}
		}
		std::cout << v;
	}
	bool AutoFlash = false;
}
void Solve();
struct ProblemData {
	bool HasTestcase = true;
	bool AutoFlash = false;
};
class Timer {
	using sc = std::chrono::steady_clock;
	sc::time_point start;
public:
	Timer() :start(sc::now()) {}
	long long elapsedMs() {
		return std::chrono::duration_cast<std::chrono::milliseconds>(sc::now() - start).count();
	}
};
class Starter {
	void Solve2() {
		TEST++;
		try { Solve(); }
		catch (int) {}
	}
	void _Start(const ProblemData& pd) {
		TEST_TOTAL = pd.HasTestcase ? _IOBase::read<int>() : 1;
		for (int t = 0; t < TEST_TOTAL; t++) {
			Solve2();
		}
	}
public:
	Starter(const ProblemData& pd) {
		std::cout.precision(17);
		std::cin.tie(nullptr);
		std::cout.tie(nullptr);
		std::ios_base::sync_with_stdio(false);
		_Start(pd);
	}
};
void initialize(ProblemData& pd);
int main(int argc, char** argv) {
	ProblemData pd;
	initialize(pd);
	_IOBase::AutoFlash = pd.AutoFlash;
	Starter starter(pd);
	return 0;
}
#define TT template<class T>
#define TTs template<class ... T>
#define TC template<class
#define TCT TC T
#define TT2 TCT, class T2>
static const bool _IS_VSCPP=
#ifdef _MSC_VER
true
#else
false
#endif
;
using Long = int64_t;
#ifdef __SIZEOF_INT128__
using longw = __int128;
#else
using longw = std::ranges::range_difference_t<std::ranges::iota_view<long long, long long>>;
#endif
using ld = long double;
using ulong = uint64_t;
using uint = uint32_t;
constexpr Long operator""v(unsigned long long a) { return (Long)a; }
constexpr ld operator""v(long double a) { return a; }
#define long Long
TT concept Invoke0 = true;
TT concept Invoke1 = true;
TT concept Invoke2 = true;
TT concept TEnumerable = requires(T a) { a.begin();	a.end(); };
TT concept TEnumerableSized = requires(T a) { a.size(); } && TEnumerable<T>;
#define FORWARD_BY(item, item_by) std::is_rvalue_reference_v<decltype(item_by)> ? std::move(item) : item
#define FORWARD(x) std::forward<decltype(x)>(x)
#define DECLTYPE(x) std::decay_t<decltype(x)>
TT using TElemFromType = DECLTYPE(*std::declval<T&>().begin());
#define TElemFromInst(v) DECLTYPE(*v.begin())
#define _DEDUCT(t_name) \
template <TEnumerableSized T> t_name(T) -> t_name<TElemFromType<T>>; \
template <TEnumerable T> t_name(T) -> t_name<TElemFromType<T>>; \
template <class ...T> t_name(T...) -> t_name<Long>;
#define ALL_TH this->begin(),this->end()
#define ALL(x) x.begin(),x.end()
#define _OPER_FROM(op) TThis operator op(const TThis& b) const { TThis a(*this); a op ## = b; return a; }
struct Empty {};
#define var auto&&
constexpr Long inf = std::numeric_limits<Long>::max() / 4;
constexpr int inf_int = std::numeric_limits<int>::max() - 10;
auto geti() { return _IOBase::read<Long>(); }
auto getc() { return _IOBase::read<char>(); }
auto get_c() { return getc(); }
auto getd() { return _IOBase::read<ld>(); }
TT void put_raw(const T& v) { _IOBase::put(v); }
void _print_endl() { _IOBase::put('\n');  if (_IOBase::AutoFlash) std::cout.flush(); }
void put_raw(const std::string& v) { _IOBase::put(v); }
void put_raw(const TEnumerable auto& v) {
	bool init = false;
	for (const auto& e : v) {
		if (!init) init = true; else put_raw(' ');
		put_raw(e);
	}
}
void put_raw_nosp(const auto& v) { put_raw(v); }
void put_raw_nosp(const TEnumerable auto& v) { for (const auto& e : v) { put_raw(e); } }
TTs void put_nosp(const T&...v) { (put_raw_nosp(v), ...); }
TTs void put(const T&...v) {
	int cnt = 0;
	((put_raw(v), (++cnt != (sizeof...(v)) ? _IOBase::put(' ') : ([]() {})())), ...);
}
TTs void print(const T&...v) { put(v...); _print_endl(); }
TTs void print_nosp(const T&...v) { put_nosp(v...); _print_endl(); }
void done() { throw 1; }
TTs void done(const T&...v) { print(v...); done(); }
TTs void print_sp(const T&...v) { put(v...); put_raw(' '); }
void print_yes(bool cond) { print(cond ? "Yes" : "No"); }
void done_yes(bool cond) { print(cond ? "Yes" : "No"); done(); }
TTs void __READ(T&... v) { ((v = _IOBase::read<T>()), ...); }
#define __read_cmn(x, ...) x __VA_ARGS__ ; __READ(__VA_ARGS__);
#define readi(...) __read_cmn(Long, __VA_ARGS__)
Long Sum1n(Long n) { if (n <= 0) return 0; return (n * n + n) / 2; }
Long Sumkn(Long k, Long n) { if (k > n) return 0;  return Sum1n(n) - Sum1n(k - 1); }
auto Iota(Long l, Long r) { return std::ranges::iota_view(l, r + 1); }
auto Iota0(Long r) { return Iota(0, r); }
auto Iota1(Long r) { return Iota(1, r); }
auto Abs(const auto& a) { return std::abs(a); }
auto Min(const auto& a, const auto& b) { return a < b ? a : b; }
auto Max(const auto& a, const auto& b) { return a > b ? a : b; }
bool Max2(var a, const auto& b) { if (b > a) { a = b; return true; } return false; }
bool Min2(var a, const auto& b) { if (b < a) { a = b; return true; } return false; }
auto Max(var a, var b, var c, var... v) { auto res = Max(a, b); Max2(res, c); (Max2(res, v), ...); return res; }
auto Min(var a, var b, var c, var... v) { auto res = Min(a, b); Min2(res, c); (Min2(res, v), ...); return res; }
TT2 auto Swap(T& a, T2& b) { return std::swap(a, b); }
TT2 auto Sort(T& a, T2& b) { if (b < a) Swap(a, b); }
auto GCD(long a, long b) { return std::gcd(a, b); }
auto LCM(long a, long b) { return std::lcm(a, b); }
auto Log(var val, long k) {
	if (k <= 1) return DECLTYPE(val) {};
	DECLTYPE(val) test = 1;
	DECLTYPE(val) cnt = 0;
	while (test < val) { test *= k; ++cnt; }
	if (test != val) { cnt--; }
	return cnt;
}
auto Log_up(var val, long k) {
	if (k <= 1) return DECLTYPE(val) {};
	DECLTYPE(val) test = 1;
	DECLTYPE(val) cnt = 0;
	while (test < val) { test *= k; ++cnt; }
	return cnt;
}
auto Pow(auto base, long exp) {
	assert(exp >= 0);
	decltype(base) result = 1;
	for (; exp > 0; exp >>= 1) {
		if (exp & 1)
			result *= base;
		base *= base;
	}
	return result;
}
auto Max(TEnumerable var v) { return *std::max_element(ALL(v)); }
auto Min(TEnumerable var t) { return *std::min_element(ALL(t)); }
auto Max(TEnumerable auto&& v, Invoke1 var f) {
	auto it = v.begin();
	auto res = f(*it);
	it++;
	for (; it != v.end(); it++) { Max2(res, f(*it)); }
	return res;
}
auto Min(TEnumerable var v, Invoke1 var f) {
	auto it = v.begin();
	auto res = f(*it);
	it++;
	for (; it != v.end(); it++) { Min2(res, f(*it)); }
	return res;
}
TC T = long > T Fact(long n) {
	static std::vector<T> stor = { 1,1 };
	if (n < stor.size()) return stor[n];
	T res = stor.back();
	long from = stor.size();
	if (n >= 400000) {
		for (; from <= n; from++) { res *= from; }
	} else {
		for (; from <= n; from++) {
			res *= from;
			stor.push_back(res);
		}
	}
	return res;
}
TC T = long> T Acomb(long k, long n) {
	assert(k <= n);
	return Fact<T>(n) / Fact<T>(n - k);
}
TC T = long > T Ccomb(long k, long n) {
	assert(k <= n);
	return Acomb<T>(k, n) / Fact<T>(k);
}
TT class _BaseCont : public T {
	using TBase = T;
public:
	using value_type = TBase::value_type;
	using reference = TBase::reference;
	using TBase::TBase;
	_BaseCont(TBase&& s)noexcept : TBase(s)  {}
	Long size() const { return TBase::size(); }
	value_type min() const { return *std::min_element(ALL_TH); }
	value_type max() const { return *std::max_element(ALL_TH); }
	value_type sum() const { return std::accumulate(ALL_TH, value_type{}); }
};
TT class _BaseContList : public _BaseCont<T> {
	using TBase = _BaseCont<T>;
public:
	using value_type = TBase::value_type;
	using reference = TBase::reference;
	using TBase::TBase;
	_BaseContList(TEnumerableSized var v) {
		TBase::resize(v.size());
		int i = 0;
		for (auto&& item : v) {
			(*this)[i++] = FORWARD_BY(item, v);
		}
	}
	_BaseContList(TEnumerable var v) {
		for (auto&& item : v) { this->push_back(FORWARD_BY(item, v)); }
	}
	void reversy() { std::reverse(ALL_TH); }
	bool contains(const value_type& v) { return std::find(ALL_TH, v) != this->end(); }
	void replace(value_type a, value_type b) { std::replace(ALL_TH, a, b); }
	void insert_at(Long i, const value_type& v) { TBase::insert(this->begin() + i, v); }
	void fill(const value_type& v) { std::fill(ALL_TH, v); }
	void remove_at(Long i) { if(i < this->size()) this->erase(this->begin() + i); }
	bool remove_first(const value_type& val) { auto itr = std::find(ALL_TH, val); if (itr != this->end()) { this->erase(itr); return true; } return false; }
	Long remove_all(const value_type& val) {
		auto newEnd = std::remove(ALL_TH, val);
		auto cnt = this->end() - newEnd;
		this->erase(newEnd, this->end());
		return cnt;
	}
	value_type pop_back() {
		auto res = std::move(TBase::back());
		TBase::pop_back();
		return res;
	}
	value_type pop_front() {
		auto res = std::move(TBase::front());
		remove_at(0);
		return res;
	}
	void push_front(const value_type& v) {
		this->insert_at(0, v);
	}
	bool is_sorted() { return std::is_sorted(ALL_TH); }
	void sort() { std::stable_sort(ALL_TH); }
	void sort(Invoke2 var f) { std::stable_sort(ALL_TH, f); }
	void sort_rev() {
		std::stable_sort(ALL_TH, std::greater{});
	}
	void set(int l, int r, const value_type& v) {
		for (int i = l; i <= r; i++) { get(i) = v; }
	}
	reference operator[](int i) { return TBase::operator[](i); }
	auto& operator[](int i) const { return TBase::operator[](i); }
	value_type& get_rev(int i) { return get(this->size()-1-i); }
	value_type& get(int i) { return (*this)[i]; }
	value_type get_def(int i, value_type def = {}) { return i < this->size() && i >= 0 ? get(i) : def; }
	void swap(int i, int j) { std::swap(get(i), get(j)); }
private:
};
TCT = Long > class Vec : public _BaseContList<std::vector<T>> {
	using TBase = _BaseContList<std::vector<T>>;
public:
	using reference = TBase::reference;
	using TBase::TBase;
	using value_type = TBase::value_type;
};
_DEDUCT(Vec);
TCT = Long > class Deque : public _BaseContList < std::deque<T>> {
	using TBase = _BaseContList < std::deque<T>>;
public:
	using TBase::TBase;
	T pop_front() {
		auto res = std::move(TBase::front());
		TBase::pop_front();
		return res;
	}
};
_DEDUCT(Deque);
TCT = long > class Cont : public Vec<T> {
	using TBase = Vec<T>;
public:
	using reference = TBase::reference;
	using TBase::TBase;
public:
	reference operator[](int i) {
		if (i >= this->size()) { this->resize(i + 1); }
		return TBase::operator[](i);
	}
};
_DEDUCT(Cont);
TCT = long > class Cont2 : public Cont<T> {
	using TBase = Cont<T>;
public:
	using TBase::TBase;
	using reference = TBase::reference;
public:
	reference operator[](int i) {
		if (i < 0) {
			return minus[-i];
		}
		return TBase::operator[](i);
	}
private:
	Cont<T> minus;
};
_DEDUCT(Cont2);
auto getv(int n, Invoke0 var getter) {
	Vec<DECLTYPE(getter())> res(n);
	for (auto i = 0; i < n; i++) {
		res[i] = getter();
	}
	return res;
}
auto getv(int n) { return getv(n, geti); }
auto gets_digit(int n = -1) {
	if (n != -1) {
		Vec res(n);
		for(int i = 0; i < n; i++) {
			res[i] = get_c() - '0';
		}
		return res;
	}
	auto s = _IOBase::read<std::string>();
	Vec res(s.size());
	for(int i = 0; i < s.size(); i++) {
		res[i] = s[i] - '0';
	}
	return res;
}
class Str : public _BaseContList<std::string> {
	using TBaseStr = std::string;
	using TBase = _BaseContList<std::string>;
	using reference = TBase::reference;
public:
	using TBase::TBase;
	using value_type = TBase::value_type;
	Str get(int l, int r) const {
		if (l > r) return "";
		if (r >= size()) r = size() - 1;
		return substr(l, r - l + 1);
	}
	void tolower() {
		std::transform(begin(), end(), begin(), [](unsigned char c) { return std::tolower(c); }
		);
	}
};
Str operator"" s(const char* _Str, size_t _Len) {
	return Str{ _Str, _Len };
}
Str gets(int n = -1) {
	if (n != -1) {
		Str res;
		res.resize(n);
		for (int i = 0; i < n; i++) {
			res[i] = get_c();
		}
		return res;
	}
	return _IOBase::read<std::string>();
}
void put_raw(const Str& v) { for (char c : v) put_raw(c); }
namespace _For_impl {
	template<class T> struct ForIter {
		using Iter = decltype(std::begin(std::declval<T&>()));
		using Iter_end = decltype(std::end(std::declval<T&>()));
		using IterRef = decltype(*std::declval<Iter&>());
		Long i = 0; Iter iter; Iter_end iter_end;
		ForIter(T& cont) :iter(std::begin(cont)), iter_end(std::end(cont)) {  }
		auto&& begin() { return std::move(*this); }
		int end() { return 0; }
		void operator++() { ++iter; ++i; }
		bool operator!=(int) { return iter != iter_end; }
		std::tuple<IterRef, Long> operator*() { return { *iter, i }; }
	};
	template<class T> struct ForIterRev {
		using Iter = decltype(std::rbegin(std::declval<T&>()));
		using Iter_end = decltype(std::rend(std::declval<T&>()));
		using IterRef = decltype(*std::declval<Iter&>());
		Long i; Iter iter; Iter_end iter_end;
		ForIterRev(T& cont) :i(cont.size()-1), iter(cont.rbegin()), iter_end(cont.rend()) {  }
		auto&& begin() { return std::move(*this); }
		int end() { return 0; }
		void operator++() { ++iter; --i; }
		bool operator!=(int) { return iter != iter_end; }
		std::tuple<IterRef, Long> operator*() { return { *iter, i }; }
	};
	template<> struct ForIter<Long> {
		Long i; Long n;
		ForIter(Long c) :n(c) { i = 0; }
		auto&& begin() { return std::move(*this); }
		int end() { return 0; }
		void operator++() { ++i; }
		bool operator!=(int) { return i != n; }
		std::tuple<Long, Long> operator*() { return { i, i }; }
	};
	template<TEnumerable T> auto Get_For_Handler_rev(T& v) {
		return ForIterRev(v);
	}
	auto Get_For_Handler(Long v) {
		return ForIter<Long>(v);
	}
	template<TEnumerable T> auto Get_For_Handler(T& v) {
		return ForIter(v);
	}
}
#define __GET_MACRO(_0,_1,_2,_3,_4,_5,X,...) X
#define __EXPAND( x ) x
#define _fe2(e,cont) for(auto&& __cont = cont; auto&& [e, e ## i] : ::_For_impl::Get_For_Handler (__cont))
#define _fer2(e,cont) for(auto&& __cont = cont; auto&& [e, e ## i] : ::_For_impl::Get_For_Handler_rev (__cont))
#define _fe1(cont) _fe2(_, cont)
#define _fer1(cont) _fer2(_, cont)
#define fe(...) __EXPAND(__GET_MACRO(_0, __VA_ARGS__, _5, _4, _3, _fe2, _fe1, _0)(__VA_ARGS__))
#define re(...) __EXPAND(__GET_MACRO(_0, __VA_ARGS__, _5, _4, _3, _fer2, _fer1, _0)(__VA_ARGS__))
void Solve() {
	auto aa = gets();
	auto bb = gets();
	auto cc = gets();
	Vec dp(2010);
	fe(e, cc) {
		auto dp2 = dp;
		for (long cnt_a = 0; cnt_a <= 2005; cnt_a++) {
			auto cnt_b = ei - cnt_a;
			if (cnt_b < 0) break;
			if (cnt_b < bb.size()) {
				auto cur = bb[cnt_b];
				long res = cur == e ? 1 : 0;
				Max2(dp2[cnt_a], dp[cnt_a] + res);
			}
			if (cnt_a < aa.size()) {
				auto cur = aa[cnt_a];
				long res = cur == e ? 1 : 0;
				Max2(dp2[cnt_a + 1], dp[cnt_a] + res);
			}
		}
		dp = dp2;
	}
	print(cc.size() - dp[aa.size()]);
}
void initialize(ProblemData& pd){
	pd.HasTestcase = true;
	pd.AutoFlash = false;
}

#include <cstdint>
#include <string>
#include <algorithm>
#include <memory>
#include <vector>
#include <iostream>
#include <functional>
#include <set>
#include <deque>
#include <chrono>
#include <ranges>
#include <random>
#include <numeric>
#include <map>
#include <cassert>
#include <queue>
#include <tuple>
#include <sstream>
#include <fstream>
#include <coroutine>
#include <array>
#include <bitset>
#include <forward_list>
#include <bit>
#include <stack>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
long TEST = 0;
long TEST_TOTAL = 0;
namespace _IOBase {
	int hook_test = 0;
	int min_tot = 0;
	std::stringstream hook_str;
	template<class T> T read() {
		T val;
		std::cin >> val;
		if (hook_test) {
			if (TEST == hook_test&&TEST_TOTAL >= std::max(min_tot, hook_test)) {
				hook_str << val;
				hook_str << " ";
			}
		}
		return val;
	}
	template<class T> void put(const T& v) {
		if (hook_test) {
			if (TEST_TOTAL >= std::max(min_tot, hook_test)) {
				if (TEST == hook_test) {
					std::cout << hook_str.str();
					hook_str = std::stringstream();
				}
				return;
			}
		}
		std::cout << v;
	}
	bool AutoFlash = false;
}
void Solve();
struct ProblemData {
	bool HasTestcase = true;
	bool AutoFlash = false;
};
class Timer {
	using sc = std::chrono::steady_clock;
	sc::time_point start;
public:
	Timer() :start(sc::now()) {}
	long long elapsedMs() {
		return std::chrono::duration_cast<std::chrono::milliseconds>(sc::now() - start).count();
	}
};
class Starter {
	void Solve2() {
		TEST++;
		try { Solve(); }
		catch (int) {}
	}
	void _Start(const ProblemData& pd) {
		TEST_TOTAL = pd.HasTestcase ? _IOBase::read<int>() : 1;
		for (int t = 0; t < TEST_TOTAL; t++) {
			Solve2();
		}
	}
public:
	Starter(const ProblemData& pd) {
		std::cout.precision(17);
		std::cin.tie(nullptr);
		std::cout.tie(nullptr);
		std::ios_base::sync_with_stdio(false);
		_Start(pd);
	}
};
void initialize(ProblemData& pd);
int main(int argc, char** argv) {
	ProblemData pd;
	initialize(pd);
	_IOBase::AutoFlash = pd.AutoFlash;
	Starter starter(pd);
	return 0;
}
#define TT template<class T>
#define TTs template<class ... T>
#define TC template<class
#define TCT TC T
#define TT2 TCT, class T2>
static const bool _IS_VSCPP=
#ifdef _MSC_VER
true
#else
false
#endif
;
using Long = int64_t;
#ifdef __SIZEOF_INT128__
using longw = __int128;
#else
using longw = std::ranges::range_difference_t<std::ranges::iota_view<long long, long long>>;
#endif
using ld = long double;
using ulong = uint64_t;
using uint = uint32_t;
constexpr Long operator""v(unsigned long long a) { return (Long)a; }
constexpr ld operator""v(long double a) { return a; }
#define long Long
TT concept Invoke0 = true;
TT concept Invoke1 = true;
TT concept Invoke2 = true;
TT concept TEnumerable = requires(T a) { a.begin();	a.end(); };
TT concept TEnumerableSized = requires(T a) { a.size(); } && TEnumerable<T>;
#define FORWARD_BY(item, item_by) std::is_rvalue_reference_v<decltype(item_by)> ? std::move(item) : item
#define FORWARD(x) std::forward<decltype(x)>(x)
#define DECLTYPE(x) std::decay_t<decltype(x)>
TT using TElemFromType = DECLTYPE(*std::declval<T&>().begin());
#define TElemFromInst(v) DECLTYPE(*v.begin())
#define _DEDUCT(t_name) \
template <TEnumerableSized T> t_name(T) -> t_name<TElemFromType<T>>; \
template <TEnumerable T> t_name(T) -> t_name<TElemFromType<T>>; \
template <class ...T> t_name(T...) -> t_name<Long>;
#define ALL_TH this->begin(),this->end()
#define ALL(x) x.begin(),x.end()
#define _OPER_FROM(op) TThis operator op(const TThis& b) const { TThis a(*this); a op ## = b; return a; }
struct Empty {};
#define var auto&&
constexpr Long inf = std::numeric_limits<Long>::max() / 4;
constexpr int inf_int = std::numeric_limits<int>::max() - 10;
auto geti() { return _IOBase::read<Long>(); }
auto getc() { return _IOBase::read<char>(); }
auto get_c() { return getc(); }
auto getd() { return _IOBase::read<ld>(); }
TT void put_raw(const T& v) { _IOBase::put(v); }
void _print_endl() { _IOBase::put('\n');  if (_IOBase::AutoFlash) std::cout.flush(); }
void put_raw(const std::string& v) { _IOBase::put(v); }
void put_raw(const TEnumerable auto& v) {
	bool init = false;
	for (const auto& e : v) {
		if (!init) init = true; else put_raw(' ');
		put_raw(e);
	}
}
void put_raw_nosp(const auto& v) { put_raw(v); }
void put_raw_nosp(const TEnumerable auto& v) { for (const auto& e : v) { put_raw(e); } }
TTs void put_nosp(const T&...v) { (put_raw_nosp(v), ...); }
TTs void put(const T&...v) {
	int cnt = 0;
	((put_raw(v), (++cnt != (sizeof...(v)) ? _IOBase::put(' ') : ([]() {})())), ...);
}
TTs void print(const T&...v) { put(v...); _print_endl(); }
TTs void print_nosp(const T&...v) { put_nosp(v...); _print_endl(); }
void done() { throw 1; }
TTs void done(const T&...v) { print(v...); done(); }
TTs void print_sp(const T&...v) { put(v...); put_raw(' '); }
void print_yes(bool cond) { print(cond ? "Yes" : "No"); }
void done_yes(bool cond) { print(cond ? "Yes" : "No"); done(); }
TTs void __READ(T&... v) { ((v = _IOBase::read<T>()), ...); }
#define __read_cmn(x, ...) x __VA_ARGS__ ; __READ(__VA_ARGS__);
#define readi(...) __read_cmn(Long, __VA_ARGS__)
Long Sum1n(Long n) { if (n <= 0) return 0; return (n * n + n) / 2; }
Long Sumkn(Long k, Long n) { if (k > n) return 0;  return Sum1n(n) - Sum1n(k - 1); }
auto Iota(Long l, Long r) { return std::ranges::iota_view(l, r + 1); }
auto Iota0(Long r) { return Iota(0, r); }
auto Iota1(Long r) { return Iota(1, r); }
auto Abs(const auto& a) { return std::abs(a); }
auto Min(const auto& a, const auto& b) { return a < b ? a : b; }
auto Max(const auto& a, const auto& b) { return a > b ? a : b; }
bool Max2(var a, const auto& b) { if (b > a) { a = b; return true; } return false; }
bool Min2(var a, const auto& b) { if (b < a) { a = b; return true; } return false; }
auto Max(var a, var b, var c, var... v) { auto res = Max(a, b); Max2(res, c); (Max2(res, v), ...); return res; }
auto Min(var a, var b, var c, var... v) { auto res = Min(a, b); Min2(res, c); (Min2(res, v), ...); return res; }
TT2 auto Swap(T& a, T2& b) { return std::swap(a, b); }
TT2 auto Sort(T& a, T2& b) { if (b < a) Swap(a, b); }
auto GCD(long a, long b) { return std::gcd(a, b); }
auto LCM(long a, long b) { return std::lcm(a, b); }
auto Log(var val, long k) {
	if (k <= 1) return DECLTYPE(val) {};
	DECLTYPE(val) test = 1;
	DECLTYPE(val) cnt = 0;
	while (test < val) { test *= k; ++cnt; }
	if (test != val) { cnt--; }
	return cnt;
}
auto Log_up(var val, long k) {
	if (k <= 1) return DECLTYPE(val) {};
	DECLTYPE(val) test = 1;
	DECLTYPE(val) cnt = 0;
	while (test < val) { test *= k; ++cnt; }
	return cnt;
}
auto Pow(auto base, long exp) {
	assert(exp >= 0);
	decltype(base) result = 1;
	for (; exp > 0; exp >>= 1) {
		if (exp & 1)
			result *= base;
		base *= base;
	}
	return result;
}
auto Max(TEnumerable var v) { return *std::max_element(ALL(v)); }
auto Min(TEnumerable var t) { return *std::min_element(ALL(t)); }
auto Max(TEnumerable auto&& v, Invoke1 var f) {
	auto it = v.begin();
	auto res = f(*it);
	it++;
	for (; it != v.end(); it++) { Max2(res, f(*it)); }
	return res;
}
auto Min(TEnumerable var v, Invoke1 var f) {
	auto it = v.begin();
	auto res = f(*it);
	it++;
	for (; it != v.end(); it++) { Min2(res, f(*it)); }
	return res;
}
TC T = long > T Fact(long n) {
	static std::vector<T> stor = { 1,1 };
	if (n < stor.size()) return stor[n];
	T res = stor.back();
	long from = stor.size();
	if (n >= 400000) {
		for (; from <= n; from++) { res *= from; }
	} else {
		for (; from <= n; from++) {
			res *= from;
			stor.push_back(res);
		}
	}
	return res;
}
TC T = long> T Acomb(long k, long n) {
	assert(k <= n);
	return Fact<T>(n) / Fact<T>(n - k);
}
TC T = long > T Ccomb(long k, long n) {
	assert(k <= n);
	return Acomb<T>(k, n) / Fact<T>(k);
}
TT class _BaseCont : public T {
	using TBase = T;
public:
	using value_type = TBase::value_type;
	using reference = TBase::reference;
	using TBase::TBase;
	_BaseCont(TBase&& s)noexcept : TBase(s)  {}
	Long size() const { return TBase::size(); }
	value_type min() const { return *std::min_element(ALL_TH); }
	value_type max() const { return *std::max_element(ALL_TH); }
	value_type sum() const { return std::accumulate(ALL_TH, value_type{}); }
};
TT class _BaseContList : public _BaseCont<T> {
	using TBase = _BaseCont<T>;
public:
	using value_type = TBase::value_type;
	using reference = TBase::reference;
	using TBase::TBase;
	_BaseContList(TEnumerableSized var v) {
		TBase::resize(v.size());
		int i = 0;
		for (auto&& item : v) {
			(*this)[i++] = FORWARD_BY(item, v);
		}
	}
	_BaseContList(TEnumerable var v) {
		for (auto&& item : v) { this->push_back(FORWARD_BY(item, v)); }
	}
	void reversy() { std::reverse(ALL_TH); }
	bool contains(const value_type& v) { return std::find(ALL_TH, v) != this->end(); }
	void replace(value_type a, value_type b) { std::replace(ALL_TH, a, b); }
	void insert_at(Long i, const value_type& v) { TBase::insert(this->begin() + i, v); }
	void fill(const value_type& v) { std::fill(ALL_TH, v); }
	void remove_at(Long i) { if(i < this->size()) this->erase(this->begin() + i); }
	bool remove_first(const value_type& val) { auto itr = std::find(ALL_TH, val); if (itr != this->end()) { this->erase(itr); return true; } return false; }
	Long remove_all(const value_type& val) {
		auto newEnd = std::remove(ALL_TH, val);
		auto cnt = this->end() - newEnd;
		this->erase(newEnd, this->end());
		return cnt;
	}
	value_type pop_back() {
		auto res = std::move(TBase::back());
		TBase::pop_back();
		return res;
	}
	value_type pop_front() {
		auto res = std::move(TBase::front());
		remove_at(0);
		return res;
	}
	void push_front(const value_type& v) {
		this->insert_at(0, v);
	}
	bool is_sorted() { return std::is_sorted(ALL_TH); }
	void sort() { std::stable_sort(ALL_TH); }
	void sort(Invoke2 var f) { std::stable_sort(ALL_TH, f); }
	void sort_rev() {
		std::stable_sort(ALL_TH, std::greater{});
	}
	void set(int l, int r, const value_type& v) {
		for (int i = l; i <= r; i++) { get(i) = v; }
	}
	reference operator[](int i) { return TBase::operator[](i); }
	auto& operator[](int i) const { return TBase::operator[](i); }
	value_type& get_rev(int i) { return get(this->size()-1-i); }
	value_type& get(int i) { return (*this)[i]; }
	value_type get_def(int i, value_type def = {}) { return i < this->size() && i >= 0 ? get(i) : def; }
	void swap(int i, int j) { std::swap(get(i), get(j)); }
private:
};
TCT = Long > class Vec : public _BaseContList<std::vector<T>> {
	using TBase = _BaseContList<std::vector<T>>;
public:
	using reference = TBase::reference;
	using TBase::TBase;
	using value_type = TBase::value_type;
};
_DEDUCT(Vec);
TCT = Long > class Deque : public _BaseContList < std::deque<T>> {
	using TBase = _BaseContList < std::deque<T>>;
public:
	using TBase::TBase;
	T pop_front() {
		auto res = std::move(TBase::front());
		TBase::pop_front();
		return res;
	}
};
_DEDUCT(Deque);
TCT = long > class Cont : public Vec<T> {
	using TBase = Vec<T>;
public:
	using reference = TBase::reference;
	using TBase::TBase;
public:
	reference operator[](int i) {
		if (i >= this->size()) { this->resize(i + 1); }
		return TBase::operator[](i);
	}
};
_DEDUCT(Cont);
TCT = long > class Cont2 : public Cont<T> {
	using TBase = Cont<T>;
public:
	using TBase::TBase;
	using reference = TBase::reference;
public:
	reference operator[](int i) {
		if (i < 0) {
			return minus[-i];
		}
		return TBase::operator[](i);
	}
private:
	Cont<T> minus;
};
_DEDUCT(Cont2);
auto getv(int n, Invoke0 var getter) {
	Vec<DECLTYPE(getter())> res(n);
	for (auto i = 0; i < n; i++) {
		res[i] = getter();
	}
	return res;
}
auto getv(int n) { return getv(n, geti); }
auto gets_digit(int n = -1) {
	if (n != -1) {
		Vec res(n);
		for(int i = 0; i < n; i++) {
			res[i] = get_c() - '0';
		}
		return res;
	}
	auto s = _IOBase::read<std::string>();
	Vec res(s.size());
	for(int i = 0; i < s.size(); i++) {
		res[i] = s[i] - '0';
	}
	return res;
}
class Str : public _BaseContList<std::string> {
	using TBaseStr = std::string;
	using TBase = _BaseContList<std::string>;
	using reference = TBase::reference;
public:
	using TBase::TBase;
	using value_type = TBase::value_type;
	Str get(int l, int r) const {
		if (l > r) return "";
		if (r >= size()) r = size() - 1;
		return substr(l, r - l + 1);
	}
	void tolower() {
		std::transform(begin(), end(), begin(), [](unsigned char c) { return std::tolower(c); }
		);
	}
};
Str operator"" s(const char* _Str, size_t _Len) {
	return Str{ _Str, _Len };
}
Str gets(int n = -1) {
	if (n != -1) {
		Str res;
		res.resize(n);
		for (int i = 0; i < n; i++) {
			res[i] = get_c();
		}
		return res;
	}
	return _IOBase::read<std::string>();
}
void put_raw(const Str& v) { for (char c : v) put_raw(c); }
namespace _For_impl {
	template<class T> struct ForIter {
		using Iter = decltype(std::begin(std::declval<T&>()));
		using Iter_end = decltype(std::end(std::declval<T&>()));
		using IterRef = decltype(*std::declval<Iter&>());
		Long i = 0; Iter iter; Iter_end iter_end;
		ForIter(T& cont) :iter(std::begin(cont)), iter_end(std::end(cont)) {  }
		auto&& begin() { return std::move(*this); }
		int end() { return 0; }
		void operator++() { ++iter; ++i; }
		bool operator!=(int) { return iter != iter_end; }
		std::tuple<IterRef, Long> operator*() { return { *iter, i }; }
	};
	template<class T> struct ForIterRev {
		using Iter = decltype(std::rbegin(std::declval<T&>()));
		using Iter_end = decltype(std::rend(std::declval<T&>()));
		using IterRef = decltype(*std::declval<Iter&>());
		Long i; Iter iter; Iter_end iter_end;
		ForIterRev(T& cont) :i(cont.size()-1), iter(cont.rbegin()), iter_end(cont.rend()) {  }
		auto&& begin() { return std::move(*this); }
		int end() { return 0; }
		void operator++() { ++iter; --i; }
		bool operator!=(int) { return iter != iter_end; }
		std::tuple<IterRef, Long> operator*() { return { *iter, i }; }
	};
	template<> struct ForIter<Long> {
		Long i; Long n;
		ForIter(Long c) :n(c) { i = 0; }
		auto&& begin() { return std::move(*this); }
		int end() { return 0; }
		void operator++() { ++i; }
		bool operator!=(int) { return i != n; }
		std::tuple<Long, Long> operator*() { return { i, i }; }
	};
	template<TEnumerable T> auto Get_For_Handler_rev(T& v) {
		return ForIterRev(v);
	}
	auto Get_For_Handler(Long v) {
		return ForIter<Long>(v);
	}
	template<TEnumerable T> auto Get_For_Handler(T& v) {
		return ForIter(v);
	}
}
#define __GET_MACRO(_0,_1,_2,_3,_4,_5,X,...) X
#define __EXPAND( x ) x
#define _fe2(e,cont) for(auto&& __cont = cont; auto&& [e, e ## i] : ::_For_impl::Get_For_Handler (__cont))
#define _fer2(e,cont) for(auto&& __cont = cont; auto&& [e, e ## i] : ::_For_impl::Get_For_Handler_rev (__cont))
#define _fe1(cont) _fe2(_, cont)
#define _fer1(cont) _fer2(_, cont)
#define fe(...) __EXPAND(__GET_MACRO(_0, __VA_ARGS__, _5, _4, _3, _fe2, _fe1, _0)(__VA_ARGS__))
#define re(...) __EXPAND(__GET_MACRO(_0, __VA_ARGS__, _5, _4, _3, _fer2, _fer1, _0)(__VA_ARGS__))
void Solve() {
	auto aa = gets();
	auto bb = gets();
	auto cc = gets();
	Vec dp(2010);
	fe(e, cc) {
		auto dp2 = dp;
		for (long cnt_a = 0; cnt_a <= 2005; cnt_a++) {
			auto cnt_b = ei - cnt_a;
			if (cnt_b < 0) break;
			if (cnt_b < bb.size()) {
				auto cur = bb[cnt_b];
				long res = cur == e ? 1 : 0;
				Max2(dp2[cnt_a], dp[cnt_a] + res);
			}
			if (cnt_a < aa.size()) {
				auto cur = aa[cnt_a];
				long res = cur == e ? 1 : 0;
				Max2(dp2[cnt_a + 1], dp[cnt_a] + res);
			}
		}
		dp = dp2;
	}
	long max = dp.max();
	print(cc.size() - max);
}
void initialize(ProblemData& pd){
	pd.HasTestcase = true;
	pd.AutoFlash = false;
}

Answer: 1


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include<bits/stdc++.h>
using namespace std;

int minChanges(string &a, string &b, string &c) {
    int n = a.length(), m = b.length();
    vector<int> prev(m + 1, 0), curr(m + 1, 0);

    // Initialize base cases
    for (int j = 0; j <= m; ++j) {
        prev[j] = j; // Only use characters from `b`
    }

    for (int i = 0; i <= n; ++i) {
        curr[0] = i; // Only use characters from `a`

        for (int j = 0; j <= m; ++j) {
            if (i == 0 && j == 0) continue; // Base case already handled
            int k = i + j - 1; // Current index in `c`

            curr[j] = INT_MAX; // Initialize to a large value

            if (i > 0 && a[i - 1] == c[k]) { // Match with `a`
                curr[j] = min(curr[j], prev[j]);
            } else if (i > 0) { // Mismatch with `a`
                curr[j] = min(curr[j], prev[j] + 1);
            }

            if (j > 0 && b[j - 1] == c[k]) { // Match with `b`
                curr[j] = min(curr[j], curr[j - 1]);
            } else if (j > 0) { // Mismatch with `b`
                curr[j] = min(curr[j], curr[j - 1] + 1);
            }
        }

        // Swap `prev` and `curr`
        swap(prev, curr);
    }

    return prev[m];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        cout << minChanges(a, b, c) << '\n';
    }

    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int f(int i,int j,int k,vector<vector<int>>&dp,string a,string b,string c){
  if(i==a.length()&&j==b.length()){
    return 0;
  }
  if(dp[i][j]!=-1)return dp[i][j];
  if(i<a.length()&&j<b.length()){
    if(a[i]==b[j]){
      if(a[i]==c[k]){
        return dp[i][j]=min(f(i+1,j,k+1,dp,a,b,c),f(i,j+1,k+1,dp,a,b,c));
      }
      return dp[i][j]=1+min(f(i+1,j,k+1,dp,a,b,c),f(i,j+1,k+1,dp,a,b,c));
    }
    if(a[i]==c[k]){
      return dp[i][j]=min(f(i+1,j,k+1,dp,a,b,c),1+f(i,j+1,k+1,dp,a,b,c));
    }
    if(b[j]==c[k]){
        return dp[i][j]=min(1+f(i+1,j,k+1,dp,a,b,c),f(i,j+1,k+1,dp,a,b,c));
    }
    return dp[i][j]= 1+min(f(i+1,j,k+1,dp,a,b,c),f(i,j+1,k+1,dp,a,b,c));
  }
  if(i<a.length()){
 if(a[i]==c[k]){
      return dp[i][j]=f(i+1,j,k+1,dp,a,b,c);
    }
    return dp[i][j]=1+f(i+1,j,k+1,dp,a,b,c);
  }
    if(b[j]==c[k]){
        return dp[i][j]=f(i,j+1,k+1,dp,a,b,c);
    }
    return dp[i][j]= 1+f(i,j+1,k+1,dp,a,b,c);

}
int main(){
    int t;
    cin>>t;
    while(t--){

       string a,b,c;
       cin>>a>>b>>c;
vector<vector<int>>dp(a.length()+1,vector<int>(b.length()+1,-1));

     cout<<  f(0,0,0,dp,a,b,c);

   cout<<endl;

    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;

//#include <random>
//mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());

//indexed-set

//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,
//tree_order_statistics_node_update> indexed_set;
//less_equal
//auto x = s.find_by_order(pos);
//int x = s.order_of_key(value)

//gp_hash_table

//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma comment(linker, "/STACK: 2000000")
typedef __int128 lll;

#define ll long long
#define ld long double
#define watch(x) cout << (#x) << " is " << (x) << endl
#define PI 3.1415926535897932384626433832795
#define EULER 2.7182818284590452353602874713
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define sz(x) (int((x).size()))

const ll mod = 1e9 + 7;
const ll INF = 1e18 ;
const ll MIN = -1e18 ;
const ll MAXII = 200005 ;

template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<typename T> void dbg(vector<T> v){for (auto x : v) cout << x << " "; cout << endl;}
template<typename T> void dbgg(pair<T, T> p){cout << p.F << " " << p.S << endl;}

string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
bool prime(ll a) { if (a==1) return 0; for (int i=2;i*i<=a;++i) if (a%i==0) return 0; return 1; }
ll inv(ll N) {if (N == 1) return 1; return (mod - ((mod / N) * inv(mod % N)) % mod) % mod;}
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }
//ll getRandomNumber(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);}
void star() { cout<<"***************************************\n"; }

//struct custom_hash {
//  static uint64_t splitmix64(uint64_t x) {
//    x += 0x9e3779b97f4a7c15;
//    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//    return x ^ (x >> 31);
//  }
//  size_t operator()(uint64_t x) const {
//    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
//    return splitmix64(x + FIXED_RANDOM);
//  }
//};
//const int mod = 1e9+7;
long long binpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

//gp_hash_table<ll, int, custom_hash> mp;

const int N = 2e3+5;
/*vector<vector<int>>adj(N);
vector<bool>vis(N);
int dfs(int i){
        vis[i]=true;
        for(auto k : adj[i]){
            if(!vis[k]) dfs(k);
        }
}*/

int n , m ;
ll dp[N][N];
string s ;
string a , b,c;
ll work(int i , int j ){
    int idx = i+j-1;
    if(dp[i][j]!=-1) return dp[i][j];
    if(i<=n && j<=m){
        ll val = (a[i]!=s[idx] ? 1 :0);
        ll ans =work(i+1,j)+val;
        val = (b[j]!=s[idx] ? 1 :0);
        ans = min(ans ,work(i,j+1)+val);
        dp[i][j]=ans;

    }
    else if(i<=n){
        ll val = (a[i]!=s[idx] ? 1 :0);
        ll ans =val+work(i+1,j);
        dp[i][j]=ans;
    }
    else if(j<=m){
         ll val = (b[j]!=s[idx] ? 1 :0);
         ll ans = val+work(i,j+1);
         dp[i][j]=ans;
    }
    else return 0;
    //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
    return dp[i][j];

}

void solve() {
    cin>>a>>b>>c;
    n= a.size(),m=b.size();
    a='#'+a;
    b='#'+b;
    c='#'+c;
    s=c;
    for(int i=0;i<=n+2;i++) for(int j=0;j<=m+2;j++) dp[i][j]=-1;

    cout<<work(1,1)<<endl;

}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);

    int tc = 1;
    cin >> tc;
    //memset(primes, 0, sizeof(primes));
//    sieve();

    while(tc--) {
        solve();
    }
}4

#include<bits/stdc++.h>
using namespace std;

//#include <random>
//mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());

//indexed-set

//#include <ext/pb_ds/assoc_container.hpp>
//using namespace __gnu_pbds;
//typedef tree<int,null_type,less<int>,rb_tree_tag,
//tree_order_statistics_node_update> indexed_set;
//less_equal
//auto x = s.find_by_order(pos);
//int x = s.order_of_key(value)

//gp_hash_table

//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;

//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
//#pragma comment(linker, "/STACK: 2000000")
typedef __int128 lll;

#define ll long long
#define ld long double
#define watch(x) cout << (#x) << " is " << (x) << endl
#define PI 3.1415926535897932384626433832795
#define EULER 2.7182818284590452353602874713
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define sz(x) (int((x).size()))

const ll mod = 1e9 + 7;
const ll INF = 1e18 ;
const ll MIN = -1e18 ;
const ll MAXII = 200005 ;

template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<typename T> void dbg(vector<T> v){for (auto x : v) cout << x << " "; cout << endl;}
template<typename T> void dbgg(pair<T, T> p){cout << p.F << " " << p.S << endl;}

string to_upper(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A'; return a; }
string to_lower(string a) { for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A'; return a; }
bool prime(ll a) { if (a==1) return 0; for (int i=2;i*i<=a;++i) if (a%i==0) return 0; return 1; }
ll inv(ll N) {if (N == 1) return 1; return (mod - ((mod / N) * inv(mod % N)) % mod) % mod;}
void yes() { cout<<"YES\n"; }
void no() { cout<<"NO\n"; }
//ll getRandomNumber(ll l, ll r) {return uniform_int_distribution<ll>(l, r)(rng);}
void star() { cout<<"***************************************\n"; }

//struct custom_hash {
//  static uint64_t splitmix64(uint64_t x) {
//    x += 0x9e3779b97f4a7c15;
//    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
//    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
//    return x ^ (x >> 31);
//  }
//  size_t operator()(uint64_t x) const {
//    static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
//    return splitmix64(x + FIXED_RANDOM);
//  }
//};
//const int mod = 1e9+7;
long long binpow(long long a, long long b, long long m) {
    a %= m;
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}

//gp_hash_table<ll, int, custom_hash> mp;

const int N = 2e3+5;
/*vector<vector<int>>adj(N);
vector<bool>vis(N);
int dfs(int i){
        vis[i]=true;
        for(auto k : adj[i]){
            if(!vis[k]) dfs(k);
        }
}*/

int n , m ;
ll dp[N][N];
string s ;
string a , b,c;
ll work(int i , int j ){
    int idx = i+j-1;
    if(dp[i][j]!=-1) return dp[i][j];
    if(i<=n && j<=m){
        ll val = (a[i]!=s[idx] ? 1 :0);
        ll ans =work(i+1,j)+val;
        val = (b[j]!=s[idx] ? 1 :0);
        ans = min(ans ,work(i,j+1)+val);
        dp[i][j]=ans;

    }
    else if(i<=n){
        ll val = (a[i]!=s[idx] ? 1 :0);
        ll ans =val+work(i+1,j);
        dp[i][j]=ans;
    }
    else if(j<=m){
         ll val = (b[j]!=s[idx] ? 1 :0);
         ll ans = val+work(i,j+1);
         dp[i][j]=ans;
    }
    else return 0;
    //cout<<i<<" "<<j<<" "<<dp[i][j]<<endl;
    return dp[i][j];

}

void solve() {
    cin>>a>>b>>c;
    n= a.size(),m=b.size();
    a='#'+a;
    b='#'+b;
    c='#'+c;
    s=c;
    for(int i=0;i<=n+2;i++) for(int j=0;j<=m+2;j++) dp[i][j]=-1;

    cout<<work(1,1)<<endl;

}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);

    int tc = 1;
    cin >> tc;
    //memset(primes, 0, sizeof(primes));
//    sieve();

    while(tc--) {
        solve();
    }
}

Answer: 1


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define float double

#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define inp(x) \
    int x;     \
    cin >> x;

#define pb push_back
#define fr(i, b) for (int i = 0; i < (int)(b); i++)
#define scanarr                 \
    int a[n];                   \
    for (int i = 0; i < n; i++) \
    cin >> a[i]
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define lower(a, b) lower_bound((a).begin(), (a).end(), (int)(b))
#define upper(a, b) upper_bound((a).begin(), (a).end(), (int)(b))
#define lower_ind(a, b) lower_bound((a).begin(), (a).end(), (int)(b)) - (a).begin()
#define upper_ind(a, b) upper_bound((a).begin(), (a).end(), (int)(b)) - (a).begin()

typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

void solve()
{
    string a;
    string b;
    string c;
    cin >> a >> b >> c;
    int n = a.size();
    int m = b.size();
    vector dp(n + 1, vector(m + 1, 0));
    for (int i = 1; i <= m; i++)
    {
        if (b[i - 1] != c[i - 1])
        {
            dp[0][i] = dp[0][i - 1] + 1;
        }
        else
        {
            dp[0][i] = dp[0][i - 1];
        }
    }
    for (int i = 1; i <= n; i++)
    {
        dp[i][0] = dp[i - 1][0];
        if (a[i - 1] != c[i - 1])
        {
            dp[i][0] += 1;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            int flag = 0;
            if (a[i - 1] == c[i + j - 1])
            {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1] + 1);
                flag = 1;
            }
            if (b[j - 1] == c[i + j - 1])
            {
                if (flag == 1)
                {
                    dp[i][j] = min(dp[i][j], dp[i][j - 1]);
                }
                else
                {
                    dp[i][j] = dp[i][j - 1];
                }
            }
            if ((a[i - 1] != c[i + j - 1]) && (b[j - 1] != c[i + j - 1]))
            {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
            }
        }
    }
    cout << dp[n][m] << endl;
}

signed main()
{
    fast;
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define float double

#define fast                          \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL)
#define inp(x) \
    int x;     \
    cin >> x;

#define pb push_back
#define fr(i, b) for (int i = 0; i < (int)(b); i++)
#define scanarr                 \
    int a[n];                   \
    for (int i = 0; i < n; i++) \
    cin >> a[i]
#define sz(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define mod 1000000007
#define lower(a, b) lower_bound((a).begin(), (a).end(), (int)(b))
#define upper(a, b) upper_bound((a).begin(), (a).end(), (int)(b))
#define lower_ind(a, b) lower_bound((a).begin(), (a).end(), (int)(b)) - (a).begin()
#define upper_ind(a, b) upper_bound((a).begin(), (a).end(), (int)(b)) - (a).begin()

typedef vector<int> vi;
typedef set<int> si;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<pair<int, int>> vpii;
typedef map<int, int> mii;
typedef map<char, int> mci;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds; // find_by_order, order_of_key

void solve()
{
    string a;
    string b;
    string c;
    cin >> a >> b >> c;
    int n = a.size();
    int m = b.size();
    vector dp(n + 1, vector(m + 1, 1e9));
    dp[0][0] = 0;
    for (int i = 1; i <= m; i++)
    {
        if (b[i - 1] != c[i - 1])
        {
            dp[0][i] = dp[0][i - 1] + 1;
        }
        else
        {
            dp[0][i] = dp[0][i - 1];
        }
    }
    for (int i = 1; i <= n; i++)
    {
        dp[i][0] = dp[i - 1][0];
        if (a[i - 1] != c[i - 1])
        {
            dp[i][0] += 1;
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (a[i - 1] == c[i + j - 1] && b[j - 1] == c[i + j - 1])
            {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]);
            }
            else if (a[i - 1] == c[i + j - 1])
            {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1] + 1);
            }
            else if (b[j - 1] == c[i + j - 1])
            {
                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j] + 1);
            }
            else if (a[i - 1] != c[i + j - 1] && b[j - 1] != c[i + j - 1])
            {
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
            }
        }
    }
    cout << dp[n][m] << endl;
}

signed main()
{
    fast;
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int dp(string &a, string &b, string &c, int ia, int ib, vector<vector<int>> &arr)
{
    if (ia == a.size() && ib == b.size())
    {
        return 0;
    }

    if (arr[ia][ib] != -1)
    {
        return arr[ia][ib];
    }

    int left = INT_MAX, right = INT_MAX;

    if (ia < a.size())
    {
        if (arr[ia + 1][ib] != -1)
        {
            left = arr[ia + 1][ib] + (a[ia] != c[ia + ib]);
        }
        else
        {
            left = dp(a, b, c, ia + 1, ib, arr) + (a[ia] != c[ia + ib]);
        }
    }
    if (ib < b.size())
    {
        if (arr[ia][ib + 1] != -1)
        {
            right = arr[ia][ib + 1] + (b[ib] != c[ia + ib]);
        }
        else
        {
            right = dp(a, b, c, ia, ib + 1, arr) + (b[ib] != c[ia + ib]);
        }
    }

    arr[ia][ib] = min(left, right);
    return arr[ia][ib];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    while (t--)
    {
        string a, b, c;
        cin >> a >> b >> c;

        vector<vector<int>> arr(a.size() + 1, vector<int>(b.size() + 1, -1));

        cout << dp(a, b, c, 0, 0, arr) << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int dp(string a, string b, string c, int ia, int ib, vector<vector<int>> arr)
{
    if (ia == a.size() && ib == b.size())
    {
        return 0;
    }

    int left = INT_MAX, right = INT_MAX;

    if (ia < a.size())
    {
        if (arr[ia][ib] != -1)
        {
            left = arr[ia + 1][ib] + (a[ia] != c[ia + ib]);
        }
        else
        {
            left = dp(a, b, c, ia + 1, ib, arr) + (a[ia] != c[ia + ib]);
        }
    }
    if (ib < b.size())
    {
        if (arr[ia][ib] != -1)
        {
            right = arr[ia][ib + 1] + (b[ib] != c[ia + ib]);
        }
        else
        {
            right = dp(a, b, c, ia, ib + 1, arr) + (b[ib] != c[ia + ib]);
        }
    }

    arr[ia][ib] = min(left, right);
    return arr[ia][ib];
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin >> t;
    while (t--)
    {
        string a, b, c;
        cin >> a >> b >> c;

        vector<vector<int>> arr(a.size() + 1, vector<int>(b.size() + 1, -1));

        cout << dp(a, b, c, 0, 0, arr) << endl;
    }

    return 0;
}

Answer: 1


#include<bits/stdc++.h>
#define int long long
#define fastio() ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
int32_t main(){
    fastio();
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        //just tabulate
        vector<vector<int>>dp((int)a.size()+1,vector<int>((int)b.size()+1));
        for(int i=(int)b.size()-1;i>=0;i--){
               int indexc=(int)c.size()-(int)b.size()+i;
               int x=0;
               if(c[indexc]!=b[i]){
               x=1;
               }
               dp[(int)a.size()][i]=x+dp[(int)a.size()][i+1];
        }
        for(int i=(int)a.size()-1;i>=0;i--){
            int indexc=(int)c.size()-(int)a.size()+i;
            int x=0;
            if(c[indexc]!=a[i]){
                x=1;
            }
            dp[i][(int)b.size()]=x+dp[i+1][(int)b.size()];
        }
        for(int i=(int)a.size()-1;i>=0;i--){
            for(int j=(int)b.size()-1;j>=0;j--){
                int indexc=(int)c.size()-(int)a.size()-(int)b.size()+i+j;
                int mine=INT_MAX;
                if(c[indexc]!=a[i]){
                  mine=min(mine,1+dp[i+1][j]);
                }
                else{
                    mine=min(mine,dp[i+1][j]);
                }
                if(c[indexc]!=b[j]){
                    mine=min(mine,1+dp[i][j+1]);
                }
                else{
                    mine=min(mine,dp[i][j+1]);
                }
                dp[i][j]=mine;
            }
        }
        cout<<dp[0][0]<<"\n";
    }
}

#include<bits/stdc++.h>
#define int long long
#define fastio() ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
using namespace std;
int dp[1001][1001];
int solve(int indexa,int indexb,int indexc,string a,string b,string c){
    int mine=INT_MAX;
     if(indexa<(int)a.size() && indexb>=(int)b.size()){
           int count=0;
           int j=indexa;
           for(int i=indexc;i<(int)c.size();i++){
                 if(c[i]!=a[j]){
                    count++;
                 }
                 j++;
           }
           mine=min(mine,count);
     }
     else if(indexa>=(int)a.size() && indexb<(int)b.size()){
           int count=0;
           int j=indexb;
           for(int i=indexc;i<(int)c.size();i++){
                 if(c[i]!=b[j]){
                    count++;
                 }
                 j++;
           }
           mine=min(mine,count);
     }
     else if(indexa<(int)a.size() && indexb<(int)b.size()){
                 if(c[indexc]!=a[indexa]){
                    if(dp[indexa+1][indexb]==-1){
                        dp[indexa+1][indexb]=solve(indexa+1,indexb,indexc+1,a,b,c);
                    }
                    mine=min(mine,1+dp[indexa+1][indexb]);
                 }
                 else{
                    if(dp[indexa+1][indexb]==-1){
                        dp[indexa+1][indexb]=solve(indexa+1,indexb,indexc+1,a,b,c);
                    }
                    mine=min(mine,dp[indexa+1][indexb]);
                 }
                 if(c[indexc]!=b[indexb]){
                     if(dp[indexa][indexb+1]==-1){
                        dp[indexa][indexb+1]=solve(indexa,indexb+1,indexc+1,a,b,c);
                    }
                    mine=min(mine,1+dp[indexa][indexb+1]);
                 }
                 else{
                      if(dp[indexa][indexb+1]==-1){
                        dp[indexa][indexb+1]=solve(indexa,indexb+1,indexc+1,a,b,c);
                    }
                    mine=min(mine,dp[indexa][indexb+1]);
                 }
     }
     else{
        mine=min(mine,(int)0);
     }
     return mine;
}
int32_t main(){
    fastio();
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        int indexa=0,indexb=0,indexc=0;
        memset(dp,-1,sizeof(dp));
        dp[indexa][indexb]=solve(indexa,indexb,indexc,a,b,c);
        cout<<dp[indexa][indexb]<<"\n";
    }
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define ulli unsigned long long int
#define ldouble  long double
#define pr pair<lli, lli>
#define loop(i,x,n) for(long long int i=x;i<n;i++)
#define loopr(i,x,n) for(lli i=x;i>=n;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define lop(x,m) for(auto &x:m)
#define pb push_back
#define SUM(x) accumulate(x.begin(),x.end(),0LL)
#define py cout<<"YES"<<endl
#define pn cout<<"NO"<<endl
#define ff first
#define ss second
#define M 1000000007
#define vi vector<lli>
#define deb(v) for(auto &x:v){cout<<x<<" ";}cout<<endl;
#define debpr(v)  for(auto x: v){cout<<x.ff<<" "<<x.ss<<"\n";}
#define inp(v,n) for(long long int i=0;i<n;i++){lli x;cin>>x;v.pb(x);}
const char nl = '\n';

#define PI 3.141592653589793238462
#define set_bit __builtin_popcountll
#define MOD1 998244353
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(lli t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ulli t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
lli powerm(lli a, lli b)
{
    lli res = 1;
    while (b > 0)
    {
        if (b & 1 != 0)
            res = (res * a) % M;
        a = (a * a) % M;
        b >>= 1;
    }
    return res;
}
lli power(lli a, lli b)
{
    lli res = 1;
    while (b > 0)
    {
        if (b & 1 != 0)
            res = (res * a) ;
        a = (a * a) ;
        b >>= 1;
    }
    return res;
}

lli inverse(lli a)
{
    return powerm(a, M - 2);
}

bool isprime(lli n){
    int f=0;
    if(n<=1)return false;
    for(lli i=2;i<=sqrt(n);i++){
        if(n%i==0){
            return false;
        }
    }
    return true;
}
bool isperfectsquare(lli x)
{
    if (x >= 0){
        lli sr = sqrt(x);
        return (sr * sr == x) ;
    }
    return false;
}

lli add(lli x,lli y)
{
    return (x%M+y%M+M)%M;
}

lli mul(lli x,lli y)
{
    return (((x%M*y%M)%M)+M)%M;
}

lli sub(lli x,lli y)
{
    return ((x%M)-(y%M)+M+M)%M;
}
lli lcm(lli a, lli b)
{
    return ((a*b) /__gcd(a, b)) ;
}

lli fact[500001];
lli inv[500001];
void factorial()
{
    fact[0]=1;
    for(int i=1; i<=500000; i++)
    {
        fact[i]=mul(fact[i-1],i);
    }
    inv[500000]=inverse(fact[500000]);
    for(int i=500000; i>0; i--)inv[i-1]=mul(inv[i],i);
}
lli ncr(lli n,lli r)
{
    if(r<0)return 0;
    lli ret=1;
    ret=fact[n];
    ret=mul(ret,inv[r]);
    ret=mul(ret,inv[n-r]);
    return ret;
}

vector<lli> primes,siev(100000+1, 0);
void sieve(){
    for(lli i=2;i<=100000;i++){
        if(siev[i]==0){
            primes.push_back(i);
            for(lli j=i*i;j<=100000;j+=i)  siev[j]=1;
        }
    }
}
bool issorted(vi v){
    loop(i,0,v.size()-1){
        if(v[i]>v[i+1]){
            return false;
        }
    }
    return true;
}

bool ispal(string s){
    lli i=0,j=s.size()-1;
    while(i<j){
        if(s[i]==s[j]){
            i++;
            j--;
        }
        else return false;
    }
    return true;
}
lli logbase(lli a, lli b)
{
    return log2(a) / log2(b);
}
lli countSquares(lli a, lli b)
{
return (floor(sqrtl(b)) - ceil(sqrtl(a)) + 1);
}
vector<vector<int>> dir4 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> dir8 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1},{1,-1},{-1,1},{1,1},{-1,-1}};
lli sqroot(lli n)
{
    lli low = 1, high = n, res;
    while (low <= high)
    {
        lli mid = (low + high) / 2;
        if ((mid * mid) == n)
        {
            res = mid;
            break;
        }
        else if ((mid * mid) > n)
        {
            high = mid - 1;
        }
        else
        {
            res = mid;
            low = mid + 1;
        }
    }
    return res;
}
vi sa,sb;
vector<vector<lli>>dp;
lli slv(lli i,lli j,string &a, string &b, string &c){
    if(i>=a.size()&&j>=b.size()){
        return 0;
    }
    if(i>=a.size()){
        return sb[j];
    }
    if(j>=b.size()){
        return sa[i];
    }
    if(dp[i][j]!=-1)return dp[i][j];
    if(a[i]==c[i+j]&&b[j]==c[i+j]){
        return dp[i][j]=min(slv(i+1,j,a,b,c),slv(i+1,j,a,b,c));
    }
    else if(a[i]==c[i+j]){
        return dp[i][j]=min(slv(i+1,j,a,b,c),1+slv(i,j+1,a,b,c));
    }
    else if(b[j]==c[i+j]){
        return dp[i][j]=min(1+slv(i+1,j,a,b,c),slv(i,j+1,a,b,c));
    }
    return dp[i][j]=min(1+slv(i+1,j,a,b,c),1+slv(i,j+1,a,b,c));
}
void solve(){
    string a,b,c;
    cin>>a>>b>>c;
    dp=vector<vector<lli>>(a.size(),vector<lli>(b.size(),-1));
    sa.resize(a.size());
    sb.resize(b.size());
    lli cc=0;
    lli j=c.size()-1;
    loopr(i,a.size()-1,0){
        if(a[i]!=c[j]){
            cc++;
        }
        j--;
        sa[i]=cc;
    }
    cc=0;
    j=c.size()-1;
    loopr(i,b.size()-1,0){
        if(b[i]!=c[j]){
            cc++;
        }
        j--;
        sb[i]=cc;
    }
    cout<<slv(0,0,a,b,c)<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("Error.txt", "w", stderr);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    std::cout.unsetf(std::ios::floatfield);
    std::cout << std::setprecision(10);
    lli t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define ulli unsigned long long int
#define ldouble  long double
#define pr pair<lli, lli>
#define loop(i,x,n) for(long long int i=x;i<n;i++)
#define loopr(i,x,n) for(lli i=x;i>=n;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define lop(x,m) for(auto &x:m)
#define pb push_back
#define SUM(x) accumulate(x.begin(),x.end(),0LL)
#define py cout<<"YES"<<endl
#define pn cout<<"NO"<<endl
#define ff first
#define ss second
#define M 1000000007
#define vi vector<lli>
#define deb(v) for(auto &x:v){cout<<x<<" ";}cout<<endl;
#define debpr(v)  for(auto x: v){cout<<x.ff<<" "<<x.ss<<"\n";}
#define inp(v,n) for(long long int i=0;i<n;i++){lli x;cin>>x;v.pb(x);}
const char nl = '\n';

#define PI 3.141592653589793238462
#define set_bit __builtin_popcountll
#define MOD1 998244353
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
void _print(lli t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ulli t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
lli powerm(lli a, lli b)
{
    lli res = 1;
    while (b > 0)
    {
        if (b & 1 != 0)
            res = (res * a) % M;
        a = (a * a) % M;
        b >>= 1;
    }
    return res;
}
lli power(lli a, lli b)
{
    lli res = 1;
    while (b > 0)
    {
        if (b & 1 != 0)
            res = (res * a) ;
        a = (a * a) ;
        b >>= 1;
    }
    return res;
}

lli inverse(lli a)
{
    return powerm(a, M - 2);
}

bool isprime(lli n){
    int f=0;
    if(n<=1)return false;
    for(lli i=2;i<=sqrt(n);i++){
        if(n%i==0){
            return false;
        }
    }
    return true;
}
bool isperfectsquare(lli x)
{
    if (x >= 0){
        lli sr = sqrt(x);
        return (sr * sr == x) ;
    }
    return false;
}

lli add(lli x,lli y)
{
    return (x%M+y%M+M)%M;
}

lli mul(lli x,lli y)
{
    return (((x%M*y%M)%M)+M)%M;
}

lli sub(lli x,lli y)
{
    return ((x%M)-(y%M)+M+M)%M;
}
lli lcm(lli a, lli b)
{
    return ((a*b) /__gcd(a, b)) ;
}

lli fact[500001];
lli inv[500001];
void factorial()
{
    fact[0]=1;
    for(int i=1; i<=500000; i++)
    {
        fact[i]=mul(fact[i-1],i);
    }
    inv[500000]=inverse(fact[500000]);
    for(int i=500000; i>0; i--)inv[i-1]=mul(inv[i],i);
}
lli ncr(lli n,lli r)
{
    if(r<0)return 0;
    lli ret=1;
    ret=fact[n];
    ret=mul(ret,inv[r]);
    ret=mul(ret,inv[n-r]);
    return ret;
}

vector<lli> primes,siev(100000+1, 0);
void sieve(){
    for(lli i=2;i<=100000;i++){
        if(siev[i]==0){
            primes.push_back(i);
            for(lli j=i*i;j<=100000;j+=i)  siev[j]=1;
        }
    }
}
bool issorted(vi v){
    loop(i,0,v.size()-1){
        if(v[i]>v[i+1]){
            return false;
        }
    }
    return true;
}

bool ispal(string s){
    lli i=0,j=s.size()-1;
    while(i<j){
        if(s[i]==s[j]){
            i++;
            j--;
        }
        else return false;
    }
    return true;
}
lli logbase(lli a, lli b)
{
    return log2(a) / log2(b);
}
lli countSquares(lli a, lli b)
{
return (floor(sqrtl(b)) - ceil(sqrtl(a)) + 1);
}
vector<vector<int>> dir4 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> dir8 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1},{1,-1},{-1,1},{1,1},{-1,-1}};
lli sqroot(lli n)
{
    lli low = 1, high = n, res;
    while (low <= high)
    {
        lli mid = (low + high) / 2;
        if ((mid * mid) == n)
        {
            res = mid;
            break;
        }
        else if ((mid * mid) > n)
        {
            high = mid - 1;
        }
        else
        {
            res = mid;
            low = mid + 1;
        }
    }
    return res;
}
vi sa,sb;
vector<vector<lli>>dp;
lli slv(lli i,lli j,string &a, string &b, string &c){
    if(i>=a.size()&&j>=b.size()){
        return 0;
    }
    if(i>=a.size()){
        return sb[j];
    }
    if(j>=b.size()){
        return sa[i];
    }
    if(dp[i][j]!=-1)return dp[i][j];
    if(a[i]==c[i+j]&&b[j]==c[i+j]){
        return dp[i][j]=min(slv(i+1,j,a,b,c),slv(i,j+1,a,b,c));
    }
    else if(a[i]==c[i+j]){
        return dp[i][j]=min(slv(i+1,j,a,b,c),1+slv(i,j+1,a,b,c));
    }
    else if(b[j]==c[i+j]){
        return dp[i][j]=min(1+slv(i+1,j,a,b,c),slv(i,j+1,a,b,c));
    }
    else return dp[i][j]=min(1+slv(i+1,j,a,b,c),1+slv(i,j+1,a,b,c));
}
void solve(){
    string a,b,c;
    cin>>a>>b>>c;
    dp=vector<vector<lli>>(a.size()+1,vector<lli>(b.size()+1,-1));
    sa.resize(a.size());
    sb.resize(b.size());
    lli cc=0;
    lli j=c.size()-1;
    loopr(i,a.size()-1,0){
        if(a[i]!=c[j]){
            cc++;
        }
        j--;
        sa[i]=cc;
    }
    cc=0;
    j=c.size()-1;
    loopr(i,b.size()-1,0){
        if(b[i]!=c[j]){
            cc++;
        }
        j--;
        sb[i]=cc;
    }
    cout<<slv(0,0,a,b,c)<<endl;
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("Error.txt", "w", stderr);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    std::cout.unsetf(std::ios::floatfield);
    std::cout << std::setprecision(10);
    lli t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define ll long long
#define pi pair<ll,ll>
#define vi vector<ll>
#define dd double
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define lcm(a, b) ((a) * (b)) / __gcd(a, b)
#define mp make_pair
#define in insert
#define fi first
#define lb lower_bound
#define ub upper_bound
#define se second
#define yes cout<<"YES"
#define no cout<<"NO"
#define rd(n) ll n; cin>>n;
#define all(v) (v).begin(),(v).end()
#define ra(a,n) vi a(n); f(i,0,n){cin>>a[i];}
const int mod = 1e9 + 7;
using namespace std;
#define int long long

const int inf = 1e9;

void solve() {
    string a, b, c;
    cin >> a >> b >> c;
    int n = a.size(), m = b.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, inf));
    dp[0][0] = 0;
    for (int i = 0; i < n; i++) {
        dp[i + 1][0] = dp[i][0] + (a[i] != c[i]);
    }
    for (int j = 0; j < m; j++) {
        dp[0][j + 1] = dp[0][j] + (b[j] != c[j]);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            dp[i][j] = min(dp[i - 1][j] + (a[i - 1] != c[i + j - 1]),
                           dp[i][j - 1] + (b[j - 1] != c[i + j - 1]));
        }
    }
    cout << dp[n][m] ;
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long t = 1;
    cin >> t;
    while (t--) {
        solve();
        cout << endl;
    }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define pi pair<ll,ll>
#define vi vector<ll>
#define dd double
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define lcm(a, b) ((a) * (b)) / __gcd(a, b)
#define mp make_pair
#define in insert
#define fi first
#define lb lower_bound
#define ub upper_bound
#define se second
#define yes cout<<"YES"
#define no cout<<"NO"
#define rd(n) ll n; cin>>n;
#define all(v) (v).begin(),(v).end()
#define ra(a,n) vi a(n); f(i,0,n){cin>>a[i];}
const int mod = 1e9 + 7;
using namespace std;
#define int long long

int n, m;

int fun(string a, string b, string c, int i, int j, vector<vector<vector<int>>>& dp) {
    if (i >= n && j >= m) return 0;
    if (dp[i][j][i + j] != -1) return dp[i][j][i + j];
    int ans = 1e5;
    if (i < n) {
        if (a[i] != c[i + j]) {
            ans = min(ans, fun(a, b, c, i + 1, j, dp) + 1);
        } else {
            ans = min(ans, fun(a, b, c, i + 1, j, dp));
        }
    }
    if (j < m) {
        if (b[j] != c[i + j]) {
            ans = min(ans, fun(a, b, c, i, j + 1, dp) + 1);
        } else {
            ans = min(ans, fun(a, b, c, i, j + 1, dp));
        }
    }
    return dp[i][j][i + j] = ans;
}

void solve() {
    string a, b, c;
    cin >> a >> b >> c;
    n = a.size(), m = b.size();
    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(m + 1, vector<int>(n + m + 1, -1)));

    cout << fun(a, b, c, 0, 0, dp);
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long t = 1;
    cin >> t;
    while (t--) {
        solve();
        cout << endl;
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
#define ll long long
#define pi pair<ll,ll>
#define vi vector<ll>
#define dd double
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fr(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define lcm(a, b) ((a) * (b)) / __gcd(a, b)
#define mp make_pair
#define in insert
#define fi first
#define lb lower_bound
#define ub upper_bound
#define se second
#define yes cout<<"YES"
#define no cout<<"NO"
#define rd(n) ll n; cin>>n;
#define all(v) (v).begin(),(v).end()
#define ra(a,n) vi a(n); f(i,0,n){cin>>a[i];}
const int mod = 1e9 + 7;
using namespace std;
#define int long long

int n, m;

int fun(string a, string b, string c, int i, int j, vector<vector<vector<int>>>& dp) {
    if (i >= n && j >= m) return 0;
    if (dp[i][j][i + j] != -1) return dp[i][j][i + j];
    int ans = 1e5;
    if (i < n) {
        if (a[i] != c[i + j]) {
            ans = min(ans, fun(a, b, c, i + 1, j, dp) + 1);
        } else {
            ans = min(ans, fun(a, b, c, i + 1, j, dp));
        }
    }
    if (j < m) {
        if (b[j] != c[i + j]) {
            ans = min(ans, fun(a, b, c, i, j + 1, dp) + 1);
        } else {
            ans = min(ans, fun(a, b, c, i, j + 1, dp));
        }
    }
    return dp[i][j][i + j] = ans;
}

void solve() {
    string a, b, c;
    cin >> a >> b >> c;
    n = a.size(), m = b.size();
    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(m + 1, vector<int>(n + m + 1, -1)));
    // cout << fun(a, b, c, 0, 0, dp);
    dp[0][0][0] = 0;

    for (int i = 0; i <= n; i++) {
        for (int j = 0; j <= m; j++) {
            for (int k = 0; k <= n + m; k++) {
                if (i + j != k) continue;
                if (i < n && k < n + m) {
                    dp[i + 1][j][k + 1] = min(dp[i + 1][j][k + 1], dp[i][j][k] + (a[i] != c[k]));
                }
                if (j < m && k < n + m) {
                    dp[i][j + 1][k + 1] = min(dp[i][j + 1][k + 1], dp[i][j][k] + (b[j] != c[k]));
                }
            }
        }
    }

    cout << dp[n][m][n + m];
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long t = 1;
    cin >> t;
    while (t--) {
        solve();
        cout << endl;
    }
    return 0;
}

Answer: 1


// #include <bits/stdc++.h>

// #include <ext/pb_ds/assoc_container.hpp>

// #include <ext/pb_ds/tree_policy.hpp>

// using namespace std;

// using namespace __gnu_pbds;

// using ll = long long;

// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

// ll dfs(string s1, string s2, string s3, ll i, ll j, vector<vector<ll>> &dp)

// {

//     if (i >= s1.size() && j >= s2.size())

//     {

//         return 0;

//     }

//     ll ans = LONG_LONG_MAX;

//     if (i < s1.size())

//     {

//         ans = dfs(s1, s2, s3, i + 1, j, dp) + (s1[i] == s3[i + j] ? 0 : 1);

//     }

//     if (j < s2.size())

//     {

//         ans = min(ans, dfs(s1, s2, s3, i, j + 1, dp) + (s2[j] == s3[i + j] ? 0 : 1));

//     }

//     return ans;

// }

// void solve()

// {

//     string s1, s2, s3;

//     cin >> s1;

//     cin >> s2;

//     cin >> s3;

//     vector<vector<ll>> dp(s1.size(), vector<ll>(s2.size(), LONG_LONG_MAX));

//     cout << dfs(s1, s2, s3, 0, 0, dp) << endl;

// }

// int main()

// {

//     ios::sync_with_stdio(false);

//     cin.tie(0);

//     long long t;

//     cin >> t;

//     while (t--)

//     {

//         long long a, b, c;

//         // cin >> a ;

//         solve();

//     }

//     return 0;

// }

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

using ll = long long;

#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

ll dfs(string &s1, string &s2, string &s3, ll i, ll j, vector<vector<ll>> &dp)

{

    if (dp[i][j] != -1)

    {

        return dp[i][j];

    }

    ll ans = LONG_LONG_MAX;

    if (i < s1.size())

    {

        // cout << "[" << i+1 << "," << j << "]" << endl;

        ans = dfs(s1, s2, s3, i + 1, j, dp) + (s1[i] == s3[i + j] ? 0 : 1);

    }

    if (j < s2.size())

    {

        // cout << "[" << i << "," << j+1 << "]" << endl;

        ans = min(ans, dfs(s1, s2, s3, i, j + 1, dp) + (s2[j] == s3[i + j] ? 0 : 1));

    }

    dp[i][j] = ans;

    return ans;

}

void solve()

{

    string s1, s2, s3;

    cin >> s1;

    cin >> s2;

    cin >> s3;

    vector<vector<ll>> dp(s1.size() + 1, vector<ll>(s2.size() + 1, -1));

    dp[s1.size()][s2.size()] = 0;

    dfs(s1, s2, s3, 0, 0, dp);

    cout << dp[0][0] << endl;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    long long t;

    cin >> t;

    while (t--)

    {

        long long a, b, c;

        // cin >> a ;

        solve();

    }

    return 0;

}

// #include <bits/stdc++.h>

// #include <ext/pb_ds/assoc_container.hpp>

// #include <ext/pb_ds/tree_policy.hpp>

// using namespace std;

// using namespace __gnu_pbds;

// using ll = long long;

// #define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

// ll dfs(string s1, string s2, string s3, ll i, ll j, vector<vector<ll>> &dp)

// {

//     if (i >= s1.size() && j >= s2.size())

//     {

//         return 0;

//     }

//     ll ans = LONG_LONG_MAX;

//     if (i < s1.size())

//     {

//         ans = dfs(s1, s2, s3, i + 1, j, dp) + (s1[i] == s3[i + j] ? 0 : 1);

//     }

//     if (j < s2.size())

//     {

//         ans = min(ans, dfs(s1, s2, s3, i, j + 1, dp) + (s2[j] == s3[i + j] ? 0 : 1));

//     }

//     return ans;

// }

// void solve()

// {

//     string s1, s2, s3;

//     cin >> s1;

//     cin >> s2;

//     cin >> s3;

//     vector<vector<ll>> dp(s1.size(), vector<ll>(s2.size(), LONG_LONG_MAX));

//     cout << dfs(s1, s2, s3, 0, 0, dp) << endl;

// }

// int main()

// {

//     ios::sync_with_stdio(false);

//     cin.tie(0);

//     long long t;

//     cin >> t;

//     while (t--)

//     {

//         long long a, b, c;

//         // cin >> a ;

//         solve();

//     }

//     return 0;

// }

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using namespace __gnu_pbds;

using ll = long long;

#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

ll dfs(string &s1, string &s2, string &s3, ll i, ll j, vector<vector<ll>> &dp)

{

    if (dp[i][j] != -1)

    {

        return dp[i][j];

    }

    ll ans = LONG_LONG_MAX;

    if (i < s1.size())

    {

        cout << "[" << i+1 << "," << j << "]" << endl;

        ans = dfs(s1, s2, s3, i + 1, j, dp) + (s1[i] == s3[i + j] ? 0 : 1);

    }

    if (j < s2.size())

    {

        cout << "[" << i << "," << j+1 << "]" << endl;

        ans = min(ans, dfs(s1, s2, s3, i, j + 1, dp) + (s2[j] == s3[i + j] ? 0 : 1));

    }

    dp[i][j] = ans;

    return ans;

}

void solve()

{

    string s1, s2, s3;

    cin >> s1;

    cin >> s2;

    cin >> s3;

    vector<vector<ll>> dp(s1.size() + 1, vector<ll>(s2.size() + 1, -1));

    dp[s1.size()][s2.size()] = 0;

    dfs(s1, s2, s3, 0, 0, dp);

    cout << dp[0][0] << endl;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0);

    long long t;

    cin >> t;

    while (t--)

    {

        long long a, b, c;

        // cin >> a ;

        solve();

    }

    return 0;

}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

void Tarsh() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

#ifdef gamed

    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);

#endif

}

typedef long long ll;

typedef long double ld;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

#define yes cout<<"YES\n";

#define no cout<<"NO\n";

#define fi first

#define se second

#define pb push_back

#define All(x) (x).begin(),(x).end()

#define Ones(n) __builtin_popcount(n)

int dx[] = {0, 0, -1, 1, -1, 1, -1, 1};

int dy[] = {-1, 1, 0, 0, 1, 1, -1, -1};

const ll MOD = 1e9 + 7;

const ll N = 1e3 + 5;

string a, b, c;

int dp[N][N];

int slv(int i, int j) {

    if (i == a.size() && j == b.size())

        return 0;

    int &ret = dp[i][j];

    if (~ret)

        return ret;

    if (i == a.size()) {

        ret = slv(i, j + 1) + (b[j] != c[i + j]);

    }

    if (j == b.size()) {

        ret = slv(i + 1, j) + (a[i] != c[i + j]);

    } else {

        ret = min(slv(i, j + 1) + (b[j] != c[i + j]), slv(i + 1, j) + (a[i] != c[i + j]));

    }

    return ret;

}

void solve() {

    cin >> a >> b >> c;

    for (int i = 0; i <= a.size(); ++i) {

        for (int j = 0; j <= b.size(); ++j) {

            dp[i][j] = -1;

        }

    }

    cout << slv(0, 0) << '\n';

}

int main() {

    Tarsh();

    int t = 1;

    cin >> t;

    while (t--) {

        solve();

    }

}

#include <bits/stdc++.h>

using namespace std;

void Tarsh() {

    ios_base::sync_with_stdio(0);

    cin.tie(0);

    cout.tie(0);

#ifdef gamed

    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);

#endif

}

typedef long long ll;

typedef long double ld;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef pair<ll, ll> pll;

#define yes cout<<"YES\n";

#define no cout<<"NO\n";

#define fi first

#define se second

#define pb push_back

#define All(x) (x).begin(),(x).end()

#define Ones(n) __builtin_popcount(n)

int dx[] = {0, 0, -1, 1, -1, 1, -1, 1};

int dy[] = {-1, 1, 0, 0, 1, 1, -1, -1};

const ll MOD = 1e9 + 7;

const ll N = 1e3 + 5;

string a, b, c;

int dp[N][N];

int slv(int i, int j) {

    if (i == a.size() && j == b.size())

        return 0;

    int &ret = dp[i][j];

    if (~ret)

        return ret;

    if (i == a.size()) {

        ret = slv(i, j + 1) + (b[j] != c[i + j]);

    }

    else if (j == b.size()) {

        ret = slv(i + 1, j) + (a[i] != c[i + j]);

    } else {

        ret = min(slv(i, j + 1) + (b[j] != c[i + j]), slv(i + 1, j) + (a[i] != c[i + j]));

    }

    return ret;

}

void solve() {

    cin >> a >> b >> c;

    for (int i = 0; i <= a.size(); ++i) {

        for (int j = 0; j <= b.size(); ++j) {

            dp[i][j] = -1;

        }

    }

    cout << slv(0, 0) << '\n';

}

int main() {

    Tarsh();

    int t = 1;

    cin >> t;

    while (t--) {

        solve();

    }

}

Answer: 2


// qdd on Dec 08, 2024

#ifdef qdd
#include <ringo>
#else
#include <bits/stdc++.h>
#define dbg(...) 42
#endif

using namespace std;

using i64 = long long;

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v) is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  bool f = 0;
  for (const T& x : v) (f ? os << ' ' : os) << x, f = 1;
  return os;
}

void sol() {
  string a, b, c;
  cin >> a >> b >> c;
  int n = a.size();
  int m = b.size();
  int l = c.size();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
  for (int k = 1; k <= l; k++) {
    for (int i = 0; i <= min(n, k); i++) {
      int j = k - i;
      if (j < 0 || j > m) continue;
      dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + 1;
      if (i > 0 && a[i - 1] == c[k - 1]) dp[i][j] = min(dp[i][j], dp[i - 1][j]);
      if (j > 0 && b[j - 1] == c[k - 1]) dp[i][j] = min(dp[i][j], dp[i][j - 1]);
    }
  }
  cout << dp[n][m] << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    sol();
  }
  return 0;
}

// qdd on Dec 08, 2024

#ifdef qdd
#include <ringo>
#else
#include <bits/stdc++.h>
#define dbg(...) 42
#endif

using namespace std;

using i64 = long long;

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v) is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  bool f = 0;
  for (const T& x : v) (f ? os << ' ' : os) << x, f = 1;
  return os;
}

void sol() {
  string a, b, c;
  cin >> a >> b >> c;
  int n = a.size();
  int m = b.size();
  int l = c.size();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1, 1e9));
  dp[0][0] = 0;
  for (int k = 1; k <= l; k++) {
    for (int i = 0; i <= min(n, k); i++) {
      int j = k - i;
      if (j < 0 || j > m) continue;
      if (i > 0) {
        dp[i][j] = min(dp[i][j], dp[i - 1][j] + (a[i - 1] != c[k - 1]));
      }
      if (j > 0) {
        dp[i][j] = min(dp[i][j], dp[i][j - 1] + (b[j - 1] != c[k - 1]));
      }
    }
  }
  cout << dp[n][m] << '\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    sol();
  }
  return 0;
}

Answer: 2


import sys
input = sys.stdin.readline

############ ---- Input Functions ---- ############
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))

def count_changes(a, b, c, i, j, k, memo_arr):
    if memo_arr[i][j] == -1:
        if k == len(c):
            memo_arr[i][j] = 0
        elif i == len(a):
            ans = 0
            for count in range(len(b[j:])):
                if b[j + count] != c[k+count]:
                    ans += 1
            memo_arr[i][j] = ans
        elif j == len(b):
            ans = 0
            for count in range(len(a[i:])):
                if a[i + count] != c[k+count]:
                    ans += 1
            memo_arr[i][j] = ans
        else:
            case1 = count_changes(a, b, c, i+1, j, k+1, memo_arr)
            if a[i] != c[k]:
                case1 += 1
            case2 = count_changes(a, b, c, i, j+1, k+1, memo_arr)
            if b[j] != c[k]:
                case2 += 1
            memo_arr[i][j] = min(case1, case2)

    return memo_arr[i][j]

t = inp()
for _ in range(t):
    a = insr()
    b = insr()
    c = insr()

    i = 0
    j = 0

    k = 0
    memo_arr = [[-1 for _ in range(len(b)+1)] for _ in range(len(a) + 1)]
    print(count_changes(a, b, c, i, j, k, memo_arr))

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10000)

############ ---- Input Functions ---- ############
def inp():
    return(int(input()))
def inlt():
    return(list(map(int,input().split())))
def insr():
    s = input()
    return(list(s[:len(s) - 1]))
def invr():
    return(map(int,input().split()))

def count_changes(a, b, c, i, j, k, memo_arr):
    if memo_arr[i][j] == -1:
        if k == len(c):
            memo_arr[i][j] = 0
        elif i == len(a):
            ans = 0
            for count in range(len(b[j:])):
                if b[j + count] != c[k+count]:
                    ans += 1
            memo_arr[i][j] = ans
        elif j == len(b):
            ans = 0
            for count in range(len(a[i:])):
                if a[i + count] != c[k+count]:
                    ans += 1
            memo_arr[i][j] = ans
        else:
            case1 = count_changes(a, b, c, i+1, j, k+1, memo_arr)
            if a[i] != c[k]:
                case1 += 1
            case2 = count_changes(a, b, c, i, j+1, k+1, memo_arr)
            if b[j] != c[k]:
                case2 += 1
            memo_arr[i][j] = min(case1, case2)

    return memo_arr[i][j]

t = inp()
for _ in range(t):
    a = insr()
    b = insr()
    c = insr()

    i = 0
    j = 0

    k = 0
    memo_arr = [[-1 for _ in range(len(b)+1)] for _ in range(len(a) + 1)]
    print(count_changes(a, b, c, i, j, k, memo_arr))

Answer: 2


#include<bits/stdc++.h>
#define int long long
using namespace std;
int helper(int index1,int index2,int index3,int n,int m,int k,string &a,string&b,string &c){
    if(index1==n&&index2==m){
        return 0;
    }
    int taken=INT_MAX;
    int taken2=INT_MAX;
    if(index1<n){
        if(a[index1]==c[index3]){
            taken=helper(index1+1,index2,index3+1,n,m,k,a,b,c);
        }
        else taken=1+helper(index1+1,index2,index3+1,n,m,k,a,b,c);
    }
    if(index2<m){
          if(b[index2]==c[index3]){
            taken2=helper(index1,index2+1,index3+1,n,m,k,a,b,c);
        }
        else taken2=1+helper(index1,index2+1,index3+1,n,m,k,a,b,c);
    }
    return min(taken,taken2);
}
 void solve(){
string a,b,c;
cin >> a >> b >> c;
int n=a.size();
int m=b.size();
int k=c.size();
int ans=helper(0,0,0,n,m,k,a,b,c);
cout << ans << endl;
 }
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
int helper(int index1,int index2,int index3,int n,int m,int k,string &a,string&b,string &c,unordered_map<string,int>&mp){
    if(index1==n&&index2==m){
        return 0;
    }
    int taken=INT_MAX;
    int taken2=INT_MAX;
    string temp=to_string(index1)+"*"+to_string(index2);
    if(mp.find(temp)!=mp.end()){
        return mp[temp];
    }
    if(index1<n){
        if(a[index1]==c[index3]){
            taken=helper(index1+1,index2,index3+1,n,m,k,a,b,c,mp);
        }
        else taken=1+helper(index1+1,index2,index3+1,n,m,k,a,b,c,mp);
    }
    if(index2<m){
          if(b[index2]==c[index3]){
            taken2=helper(index1,index2+1,index3+1,n,m,k,a,b,c,mp);
        }
        else taken2=1+helper(index1,index2+1,index3+1,n,m,k,a,b,c,mp);
    }
    return mp[temp]=min(taken,taken2);
}
 void solve(){
string a,b,c;
cin >> a >> b >> c;
int n=a.size();
int m=b.size();
int k=c.size();
unordered_map<string,int>mp;
int ans=helper(0,0,0,n,m,k,a,b,c,mp);
cout << ans << endl;
 }
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int t;
    cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define int long long int
using namespace std;

void solve(){

    string a,b,c;
    cin>>a>>b>>c;

    int l = a.size(), m = b.size(), n = c.size();
    vector<vector<int>> dp(l+2, vector<int>(n+2, INT_MAX));

    dp[0][0] = 0;
    for(int i=1, j=0; i<=n ; i++){
        if(i<=m && b[i-1]!=c[i-1])
            j++;
        dp[0][i] = j;
    }

    for(int i=1 ; i<=l ; i++){
        for(int j=i ; j<=i+m ; j++){

            int val = dp[i-1][j-1] + (a[i-1]!=c[j-1]?1:0);
            val = min(val, dp[i][j-1] + (b[j-i-1]!=c[j-1]?1:0));
            dp[i][j] = val;

        }
    }

    // for(int i=0;i<=l;i++){
    //     for(int j=0;j<=n;j++)cout<<dp[i][j]<<" ";
    //     cout<<endl;
    // }
    cout<<dp[l][n]<<'\n';

}

signed main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
#define int long long int
using namespace std;

void solve(){

    string a,b,c;
    cin>>a>>b>>c;

    int l = a.size(), m = b.size(), n = c.size();
    vector<vector<int>> dp(l+2, vector<int>(n+2, 99));

    dp[0][0] = 0;
    for(int i=0, j=0; i<n ; i++){
        if(j<m && b[j]==c[i])
            j++;
        dp[0][i+1] = i-j+1;
    }

    for(int i=1 ; i<=l ; i++){
        for(int j=i ; j<=i+m ; j++){

            int val = dp[i-1][j-1] + (a[i-1]!=c[j-1]?1:0);
            if(j<=i+m){
                val = min(val, dp[i][j-1] + (b[j-i-1]!=c[j-1]?1:0));
            }
            dp[i][j] = val;

        }
    }

    // for(int i=0;i<=l;i++){
    //     for(int j=0;j<=n;j++)cout<<dp[i][j]<<" ";
    //     cout<<endl;
    // }
    cout<<dp[l][n]<<'\n';

}

signed main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}

Answer: 1