    #include <numeric>

    #include <complex>

    #include <queue>

    #include <set>

    #include <unordered_set>

    #include <list>

    #include <chrono>

    #include <random>

    #include <iostream>

    #include <algorithm>

    #include <cmath>

    #include <string>

    #include <vector>

    #include <map>

    #include <unordered_map>

    #include <stack>

    #include <iomanip>

    #include <fstream>

    #include <cstring>

    #include <climits>

    #include <bitset>

    #include <ext/pb_ds/assoc_container.hpp>

    #include <ext/pb_ds/tree_policy.hpp>

    #define fastio() ios::sync_with_stdio(NULL);cin.tie(0);cout.tie(0);

    #define   int long long int

      using namespace std ;

    //  using namespace __gnu_pbds;

    // typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;

    int mod = 998244353;

    int gcd(int a, int b)

    {

        while (a > 0 && b > 0) {

            if (a > b) {

                a = a % b;

            }

            else {

                b = b % a;

            }

        }

        if (a == 0) {

            return b;

        }

        return a;

    }

    pair<int,int> overLap(pair<int ,int > a , pair<int,int> b )

    {

        int l = max(a.first,  b.first);

        int r = min(a.second , b.second) ; ;

    if(l>r)return {-1, -1 };

    return {l,r};

        return {-1, -1 };

    }

    int LCM(int a , int b ){

        int ans = (a*b)/(gcd(a,b)) ;

        return ans ;

    }

    void print(vector<int> &arr){

        for(auto it : arr){

            cout<<it<<" ";

        }

        cout<<"\n";

    }

    void print(vector<pair<string ,string >> arr){

        for(auto it: arr ){

                cout<<it.first<<" "<<it.second<<"\n";

            }

    }

    void print(vector<vector<int>>arr){

        for(auto it :arr){

            for(auto itr : it){

                cout<<itr<<" ";

            }

            cout<<"\n";

        }

    }

    void print(vector<vector<char>>arr){

        for(auto it :arr){

            for(auto itr : it){

                cout<<itr<<" ";

            }

            cout<<"\n";

        }

    }

    void print(vector<string> &arr){

        for(auto it : arr){

            cout<<it<<"\n";

        }

       // cout<<"\n";

    }

    void print(vector<pair<int,int>>&mat){

        for(auto itr : mat){

                cout<<itr.first<< "--"<<itr.second<< " "<<"\n";

        }

    }

    void print(unordered_map<int ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(vector<pair<double,int>>&mat){

        for(auto it :mat){

            cout<<it.first<<" "<<it.second<<"\n";

        }

    }

    void print(map<int ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(map<string ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(unordered_map<string ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(map<int ,int ,greater<int> > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    int ncr(int n , int r ){

        int  sum = 1;

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

                sum = sum * (n - r + i) / i;

            }

            return sum ;

    }

    int mex(vector<int>arr){

    map<int,int>mp ;

    for(auto it : arr){

        mp[it]++ ;

    }

    vector<int>t ;

    for(auto  it : mp){

        t.push_back(it.first) ;

    }

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

        if(t[i]!=i){

            return i;

        }

    }

    return t.size() ;

    }

    void print(map<char ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    int power ( int base , int pow , int mod  ){

        if(pow==0)

        return 1   ;

        if(pow==1)

        return  base  ;

        if(pow%2==0){

            int val = (power(base , pow/2, mod ))%mod;

            return (val*val)%mod ;

        }

        return    (base*(power(base , pow-1,mod )%mod)) %mod;

    }

    //  bool compare(vector<int>&a , vector<int>&b ){

    //     if(a[1]==b[1])

    //     {

    //         return a[0]<b[0];

    //     }

    //     return a[1]<b[1] ;

    // }

    void print(map<string , pair<int,char >>mp){

        for(auto it : mp){

            cout<<it.first<<"->"<<"freq-"<<it.second.first<<"char"<<it.second.second<<"\n";

        }

    }

    void get_tree(vector<int>arr[], int s , vector<int>&tree, vector<int>&vis ){

        tree.push_back(s);

        vis[s]= 1 ;

        for(auto it : arr[s]){

            if(vis[it]==0){

                get_tree(arr, it, tree, vis );

            }

        }

    }

    int  build_segment_tree(vector<int>&tree, vector<int>&arr,int indx ,  int s , int e){

        if(s>e)return INT_MIN;

        if(s==e){

            tree[indx]=arr[s];

            return tree[indx];

        }

        int mid = (s+e)/2 ;

       int l= build_segment_tree(tree, arr, 2*indx+1, s ,mid ) ;

       int r= build_segment_tree(tree, arr, 2*indx+2, mid+1, e ) ;

        tree[indx]=l&r ;

        return l&r ;

    }

    int query_segment_tree(vector<int>&tree, vector<int>&arr,int indx ,int s ,int e , int L ,int R ){

        if(s>=L and e<=R){

            return tree[indx];

        }

        if(s>R || e<L){

            return (1<<30) -1 ;

        }

        int mid = (s+e)/2 ;

        int left =  query_segment_tree(tree, arr, 2*indx+1, s, mid , L ,R ) ;

        int right=query_segment_tree(tree,arr, 2*indx+2, mid+1 , e , L , R) ;

        // left=left==INT_MIN?0:left ;

        // right=right==INT_MIN?0:right;

        return left&right;

    }

    int getsum(int l ,int r ){

        int n = r-l+1 ;

        return (l+r)*(n)/2 ;

    }

    int SUM(int n) {

        return ((n+1)*(n))/2 ;

    }

    bool overlap(int a, int b ,int c ,int d ){

        //1 2 1 3

        int mn = max(a, c )  ;

        int mx =min(b,d) ;

        if(mn>mx)return false ;

        return true ;

    }

    void print(queue<vector<int>>q){

        while(!q.empty()){

            auto it = q.front() ;

            q.pop() ;

            for(auto itr:it){

                cout<<itr<<" ";

            }

        }

        cout<<"nl\n";

    }

    void print(multiset<int>mt){

        for(auto it :mt){

            cout<<it<<" ";

        }

        cout<<"\n";

    }

    bool isPrime(int n ){

        for(int i =2 ; i*i<=n ; i++){

            if(n%i==0)return true ;

        }

        return false ;

    }

    void heapify(vector<int>&arr , int i ){

        // max heap ;

        int largest  = i ;

        int l = 2*i+1 ;

        int r = 2*i+2  ;

        int n = arr.size() ;

        if(l<n and arr[l]>arr[i] ){

            largest = l ;

        }

        if(r<n and arr[r]>arr[largest]){

            largest= r ;

        }

        if(largest!=i){

            swap(arr[i], arr[largest]);

            heapify( arr, largest) ;

        }

    }

    string to_binary(int a ){

    //     std::string binary = std::bitset<8>(128).to_string(); //to binary

    // std::cout<<binary<<"\n";

    // return " ";

        string res = bitset<16>(a).to_string() ;

        return res ;

    }

    void print(vector<vector<pair<int,int>>>&adj){

        int cnt =0;

         for(auto it : adj){

        cout<<cnt<<"->";

        cnt++ ;

        for(auto itr : it){

            cout<<"("<<itr.first<<" "<<itr.second<<")" <<" ";

        }

        cout<<"\n";

       }

    }

    void buildHeap(vector<int>&arr){

        int n = arr.size() ;

        int internal_nodes  = (n/2)-1 ;

        for(int i = internal_nodes ; i>=0 ; i-- ){

            heapify(arr, i) ;

        }

        //print(arr) ;

    }

    int msb(int a ){

        for(int i =31 ; i>=0 ; i--){

            int mask = 1<<i ;

            int d = mask&a ;

            if(d)return i ;

        }

        return 0 ;

    }

    int unsetbit(int n, int k){

        return (n & ~(1 << (k)));

    }

    int setbit(int n, int k)

    {

        // kth bit of n is being set by this operation

        return ((1 << k) | n);

    }

    bool reflectHor(vector<vector<int>>arr, map<vector<vector<int> >,int > &mp){

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

            arr[i][1]=arr[i][1]*(-1) ;

        }

         if( mp.find(arr)!=mp.end() ){

            return false ;

         }

         return true ;

    }

     bool reflectVer(vector<vector<int>>arr, map<vector<vector<int> >,int > &mp){

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

            arr[i][0]=arr[i][0]*(-1) ;

        }

         if( mp.find(arr)!=mp.end() ){

            return false ;

         }

         return true ;

    }

    bool isValid(vector<vector<int>>&arr, map<vector<vector<int>>, int > &mp){

        int n = arr.size() ;

        for(int i =0 ; i<4 ; i++){

            for(int j=0 ; j<n ; j++){

                swap(arr[j][0], arr[j][1]);

                arr[j][i]=arr[j][1]*(-1) ;

            }

            if(mp.find(arr)!=mp.end()){

                return false ;

            }

            // reflect  ;

            bool bb = reflectHor(arr,mp ) ;

            if(bb==false)return false ;

            bb= reflectVer(arr, mp) ;

            if(bb==false)return false ;

        }

        return true ;

    }

    bool compare1(int a, int b ){

       if(a<b)return true ;

       return false ;

    }

    class DSU {

        vector<int>parent ;

        vector<int>rank ;

    public:

          DSU(int n ){

            parent.resize(n+5) ;

            rank.resize(n+5,0) ;

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

                parent[i]=i ;

            }

        }

        int findParent(int val ){

            if(val==parent[val])

            return val ;

            return parent[val]= findParent(parent[val]);

        }

        void unionByRank(int u , int v ){

            if(rank[u]>rank[v]){

                parent[v]=u ;

            }

            else if(rank[u]<rank[v]){

                parent[u]=v ;

            }

            else {

                parent[u]=v ;

                rank[v]++ ;

            }

        }

    };

    void dfs(map<int, set<int>>&mat, vector<int>&vis, int src ){

        vis[src]=1 ;

        //cout<<src<<" ; ";

        for(auto ngbr: mat[src]){

            if(vis[ngbr]==0){

                dfs(mat, vis, ngbr) ;

            }

        }

    }

    int help(map<int,set<int>>&mat, int n , int v){

        vector<vector<int> >arr ;

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

        int a = i;

        if(i==v)continue  ;

        if(mat.find(i)!=mat.end())

        {

        int b = mat[i].size()  ;

        arr.push_back({b,a} ) ;

        }

       }

       sort(arr.begin(), arr.end()) ;

       reverse(arr.begin(),arr.end()) ;

       //print(arr) ;

       int v1 = arr[0][1] ;

       for(auto it: mat[v1]){

         //cout<<it<<" "<<"erase \n";

          mat[it].erase(v1) ;

       }

       mat.erase(v1) ;

       return v1 ;

    }

    void check(int x =1){

        cout<<x<<" "<<"check\n";

    }

    int fun(string&a, string&b, string&c , int i, int j , int k1,vector<vector<int>>&dp ){

        int k = i+j ;

        if(k>=c.size())return 0 ;

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

            //

            int cnt =0 ;

            while(k<c.size()){

                if(c[k]!=b[j]){

                    cnt++ ;

                }

                k++ ;

                j++ ;

            }

            return cnt ;

        }

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

            int cnt =0 ;

            while(k<c.size()){

                if(c[k]!=a[i]){

                    cnt++ ;

                }

                k++ ;

                i++ ;

            }

            return cnt ;

        }

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

        if(a[i]==b[j] and b[j]==c[k]){

            int op1 = fun(a ,b,c , i+1, j , i+j+1 ,dp) ;

            int op2 = fun(a, b, c , i, j+1, i+j+1,dp) ;

            // int op3 = fun(a ,b,c , i+1, j , i+j+1,dp )+1 ;

            // int op4 = fun(a, b, c , i, j+1, i+j+1,dp)+1 ;

            return dp[i][j+i]=min({op1,op2}) ;

        }

        else if(a[i]==c[k]){

            int op1 =  fun(a,b,c, i+1, j , i+j+1,dp) ;

            int op2 = fun(a, b,c ,i, j+1, i+j+1,dp) +1 ;

            return dp[i][j+i]=min(op1,op2) ;

        }

        else if(b[j]==c[k]){

            int op1 =  fun(a, b,c,i, j+1, i+j+1,dp );

            int op2 = fun(a, b, c, i+1 , j, i+j+1,dp) +1 ;

            return dp[i][j+i]=min(op1,op2) ;

        }

        else {

            int op1 = fun(a, b, c, i+1, j , i+j+1,dp) ;

            int op2 = fun(a,b, c, i, j+1, i+j+1 ,dp);

            return dp[i][j+i]=min(op1,op2)+1 ;

        }

        return 0 ;

    }

    void solve()

    {

      string a , b,c ;

      cin>>a>>b>>c ;

      int n = a.size() ;

      vector<vector<int>>dp(n, vector<int>(n+1+n+1,-1));

      int ans = fun(a,b, c, 0 , 0 , 0, dp) ;

      int res = -2 ;

      for(int i=0 ; i<2*n+2; i++){

        res = max(res, dp[n-1][i]);

      }

      cout<<res<<"\n";

      //print(dp);

    }

    signed main()

    {

    # ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    cout << "Reading from input.txt\n";

    freopen("output.txt", "w", stdout);

    if (!freopen("input.txt", "r", stdin)) {

        cerr << "Could not open input.txt\n";

        return 1;

    }

    #endif

        //fastio()  ;

        int  t;

    cin >> t;

    // t=1 ;

    for(int it=1;it<=t;it++) {

        // if(it==246){

        //      int n , m ;

        //      cin>>n>>m ;

        //      string s ="" ;

        //      for(int i =0 ; i<n ; i++){

        //         string a ;

        //         cin>>a ;

        //         s+=a ;

        //         s+='0';

        //      }

        //      vector<int>ans ;

        //      for(int i = 0 ; i<m ; i++){

        //             int  x ;

        //             cin>>x ;

        //             ans.push_back(x) ; xv

        //      }

        //      cout<<s<<" ";

        //      for(auto it : ans )cout<<it<<"\n";

        // }

        // else

        solve();

    }

    return 0;

    }

    #include <numeric>

    #include <complex>

    #include <queue>

    #include <set>

    #include <unordered_set>

    #include <list>

    #include <chrono>

    #include <random>

    #include <iostream>

    #include <algorithm>

    #include <cmath>

    #include <string>

    #include <vector>

    #include <map>

    #include <unordered_map>

    #include <stack>

    #include <iomanip>

    #include <fstream>

    #include <cstring>

    #include <climits>

    #include <bitset>

    #include <ext/pb_ds/assoc_container.hpp>

    #include <ext/pb_ds/tree_policy.hpp>

    #define fastio() ios::sync_with_stdio(NULL);cin.tie(0);cout.tie(0);

    #define   int long long int

      using namespace std ;

    //  using namespace __gnu_pbds;

    // typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;

    int mod = 998244353;

    int gcd(int a, int b)

    {

        while (a > 0 && b > 0) {

            if (a > b) {

                a = a % b;

            }

            else {

                b = b % a;

            }

        }

        if (a == 0) {

            return b;

        }

        return a;

    }

    pair<int,int> overLap(pair<int ,int > a , pair<int,int> b )

    {

        int l = max(a.first,  b.first);

        int r = min(a.second , b.second) ; ;

    if(l>r)return {-1, -1 };

    return {l,r};

        return {-1, -1 };

    }

    int LCM(int a , int b ){

        int ans = (a*b)/(gcd(a,b)) ;

        return ans ;

    }

    void print(vector<int> &arr){

        for(auto it : arr){

            cout<<it<<" ";

        }

        cout<<"\n";

    }

    void print(vector<pair<string ,string >> arr){

        for(auto it: arr ){

                cout<<it.first<<" "<<it.second<<"\n";

            }

    }

    void print(vector<vector<int>>arr){

        for(auto it :arr){

            for(auto itr : it){

                cout<<itr<<" ";

            }

            cout<<"\n";

        }

    }

    void print(vector<vector<char>>arr){

        for(auto it :arr){

            for(auto itr : it){

                cout<<itr<<" ";

            }

            cout<<"\n";

        }

    }

    void print(vector<string> &arr){

        for(auto it : arr){

            cout<<it<<"\n";

        }

       // cout<<"\n";

    }

    void print(vector<pair<int,int>>&mat){

        for(auto itr : mat){

                cout<<itr.first<< "--"<<itr.second<< " "<<"\n";

        }

    }

    void print(unordered_map<int ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(vector<pair<double,int>>&mat){

        for(auto it :mat){

            cout<<it.first<<" "<<it.second<<"\n";

        }

    }

    void print(map<int ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(map<string ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(unordered_map<string ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    void print(map<int ,int ,greater<int> > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    int ncr(int n , int r ){

        int  sum = 1;

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

                sum = sum * (n - r + i) / i;

            }

            return sum ;

    }

    int mex(vector<int>arr){

    map<int,int>mp ;

    for(auto it : arr){

        mp[it]++ ;

    }

    vector<int>t ;

    for(auto  it : mp){

        t.push_back(it.first) ;

    }

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

        if(t[i]!=i){

            return i;

        }

    }

    return t.size() ;

    }

    void print(map<char ,int > m ){

        for(auto it : m ){

            cout<<it.first<<"->"<<it.second<<" ";

        }

        cout<<"\n" ;

    }

    int power ( int base , int pow , int mod  ){

        if(pow==0)

        return 1   ;

        if(pow==1)

        return  base  ;

        if(pow%2==0){

            int val = (power(base , pow/2, mod ))%mod;

            return (val*val)%mod ;

        }

        return    (base*(power(base , pow-1,mod )%mod)) %mod;

    }

    //  bool compare(vector<int>&a , vector<int>&b ){

    //     if(a[1]==b[1])

    //     {

    //         return a[0]<b[0];

    //     }

    //     return a[1]<b[1] ;

    // }

    void print(map<string , pair<int,char >>mp){

        for(auto it : mp){

            cout<<it.first<<"->"<<"freq-"<<it.second.first<<"char"<<it.second.second<<"\n";

        }

    }

    void get_tree(vector<int>arr[], int s , vector<int>&tree, vector<int>&vis ){

        tree.push_back(s);

        vis[s]= 1 ;

        for(auto it : arr[s]){

            if(vis[it]==0){

                get_tree(arr, it, tree, vis );

            }

        }

    }

    int  build_segment_tree(vector<int>&tree, vector<int>&arr,int indx ,  int s , int e){

        if(s>e)return INT_MIN;

        if(s==e){

            tree[indx]=arr[s];

            return tree[indx];

        }

        int mid = (s+e)/2 ;

       int l= build_segment_tree(tree, arr, 2*indx+1, s ,mid ) ;

       int r= build_segment_tree(tree, arr, 2*indx+2, mid+1, e ) ;

        tree[indx]=l&r ;

        return l&r ;

    }

    int query_segment_tree(vector<int>&tree, vector<int>&arr,int indx ,int s ,int e , int L ,int R ){

        if(s>=L and e<=R){

            return tree[indx];

        }

        if(s>R || e<L){

            return (1<<30) -1 ;

        }

        int mid = (s+e)/2 ;

        int left =  query_segment_tree(tree, arr, 2*indx+1, s, mid , L ,R ) ;

        int right=query_segment_tree(tree,arr, 2*indx+2, mid+1 , e , L , R) ;

        // left=left==INT_MIN?0:left ;

        // right=right==INT_MIN?0:right;

        return left&right;

    }

    int getsum(int l ,int r ){

        int n = r-l+1 ;

        return (l+r)*(n)/2 ;

    }

    int SUM(int n) {

        return ((n+1)*(n))/2 ;

    }

    bool overlap(int a, int b ,int c ,int d ){

        //1 2 1 3

        int mn = max(a, c )  ;

        int mx =min(b,d) ;

        if(mn>mx)return false ;

        return true ;

    }

    void print(queue<vector<int>>q){

        while(!q.empty()){

            auto it = q.front() ;

            q.pop() ;

            for(auto itr:it){

                cout<<itr<<" ";

            }

        }

        cout<<"nl\n";

    }

    void print(multiset<int>mt){

        for(auto it :mt){

            cout<<it<<" ";

        }

        cout<<"\n";

    }

    bool isPrime(int n ){

        for(int i =2 ; i*i<=n ; i++){

            if(n%i==0)return true ;

        }

        return false ;

    }

    void heapify(vector<int>&arr , int i ){

        // max heap ;

        int largest  = i ;

        int l = 2*i+1 ;

        int r = 2*i+2  ;

        int n = arr.size() ;

        if(l<n and arr[l]>arr[i] ){

            largest = l ;

        }

        if(r<n and arr[r]>arr[largest]){

            largest= r ;

        }

        if(largest!=i){

            swap(arr[i], arr[largest]);

            heapify( arr, largest) ;

        }

    }

    string to_binary(int a ){

    //     std::string binary = std::bitset<8>(128).to_string(); //to binary

    // std::cout<<binary<<"\n";

    // return " ";

        string res = bitset<16>(a).to_string() ;

        return res ;

    }

    void print(vector<vector<pair<int,int>>>&adj){

        int cnt =0;

         for(auto it : adj){

        cout<<cnt<<"->";

        cnt++ ;

        for(auto itr : it){

            cout<<"("<<itr.first<<" "<<itr.second<<")" <<" ";

        }

        cout<<"\n";

       }

    }

    void buildHeap(vector<int>&arr){

        int n = arr.size() ;

        int internal_nodes  = (n/2)-1 ;

        for(int i = internal_nodes ; i>=0 ; i-- ){

            heapify(arr, i) ;

        }

        //print(arr) ;

    }

    int msb(int a ){

        for(int i =31 ; i>=0 ; i--){

            int mask = 1<<i ;

            int d = mask&a ;

            if(d)return i ;

        }

        return 0 ;

    }

    int unsetbit(int n, int k){

        return (n & ~(1 << (k)));

    }

    int setbit(int n, int k)

    {

        // kth bit of n is being set by this operation

        return ((1 << k) | n);

    }

    bool reflectHor(vector<vector<int>>arr, map<vector<vector<int> >,int > &mp){

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

            arr[i][1]=arr[i][1]*(-1) ;

        }

         if( mp.find(arr)!=mp.end() ){

            return false ;

         }

         return true ;

    }

     bool reflectVer(vector<vector<int>>arr, map<vector<vector<int> >,int > &mp){

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

            arr[i][0]=arr[i][0]*(-1) ;

        }

         if( mp.find(arr)!=mp.end() ){

            return false ;

         }

         return true ;

    }

    bool isValid(vector<vector<int>>&arr, map<vector<vector<int>>, int > &mp){

        int n = arr.size() ;

        for(int i =0 ; i<4 ; i++){

            for(int j=0 ; j<n ; j++){

                swap(arr[j][0], arr[j][1]);

                arr[j][i]=arr[j][1]*(-1) ;

            }

            if(mp.find(arr)!=mp.end()){

                return false ;

            }

            // reflect  ;

            bool bb = reflectHor(arr,mp ) ;

            if(bb==false)return false ;

            bb= reflectVer(arr, mp) ;

            if(bb==false)return false ;

        }

        return true ;

    }

    bool compare1(int a, int b ){

       if(a<b)return true ;

       return false ;

    }

    class DSU {

        vector<int>parent ;

        vector<int>rank ;

    public:

          DSU(int n ){

            parent.resize(n+5) ;

            rank.resize(n+5,0) ;

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

                parent[i]=i ;

            }

        }

        int findParent(int val ){

            if(val==parent[val])

            return val ;

            return parent[val]= findParent(parent[val]);

        }

        void unionByRank(int u , int v ){

            if(rank[u]>rank[v]){

                parent[v]=u ;

            }

            else if(rank[u]<rank[v]){

                parent[u]=v ;

            }

            else {

                parent[u]=v ;

                rank[v]++ ;

            }

        }

    };

    void dfs(map<int, set<int>>&mat, vector<int>&vis, int src ){

        vis[src]=1 ;

        //cout<<src<<" ; ";

        for(auto ngbr: mat[src]){

            if(vis[ngbr]==0){

                dfs(mat, vis, ngbr) ;

            }

        }

    }

    int help(map<int,set<int>>&mat, int n , int v){

        vector<vector<int> >arr ;

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

        int a = i;

        if(i==v)continue  ;

        if(mat.find(i)!=mat.end())

        {

        int b = mat[i].size()  ;

        arr.push_back({b,a} ) ;

        }

       }

       sort(arr.begin(), arr.end()) ;

       reverse(arr.begin(),arr.end()) ;

       //print(arr) ;

       int v1 = arr[0][1] ;

       for(auto it: mat[v1]){

         //cout<<it<<" "<<"erase \n";

          mat[it].erase(v1) ;

       }

       mat.erase(v1) ;

       return v1 ;

    }

    void check(int x =1){

        cout<<x<<" "<<"check\n";

    }

    int fun(string&a, string&b, string&c , int i, int j , int k1,vector<vector<int>>&dp ){

        int k = i+j ;

        if(k>=c.size())return 0 ;

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

            //

            int cnt =0 ;

            while(k<c.size()){

                if(c[k]!=b[j]){

                    cnt++ ;

                }

                k++ ;

                j++ ;

            }

            return cnt ;

        }

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

            int cnt =0 ;

            while(k<c.size()){

                if(c[k]!=a[i]){

                    cnt++ ;

                }

                k++ ;

                i++ ;

            }

            return cnt ;

        }

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

        if(a[i]==b[j] and b[j]==c[k]){

            int op1 = fun(a ,b,c , i+1, j , i+j+1 ,dp) ;

            int op2 = fun(a, b, c , i, j+1, i+j+1,dp) ;

            // int op3 = fun(a ,b,c , i+1, j , i+j+1,dp )+1 ;

            // int op4 = fun(a, b, c , i, j+1, i+j+1,dp)+1 ;

            return dp[i][j]=min({op1,op2}) ;

        }

        else if(a[i]==c[k]){

            int op1 =  fun(a,b,c, i+1, j , i+j+1,dp) ;

            int op2 = fun(a, b,c ,i, j+1, i+j+1,dp) +1 ;

            return dp[i][j]=min(op1,op2) ;

        }

        else if(b[j]==c[k]){

            int op1 =  fun(a, b,c,i, j+1, i+j+1,dp );

            int op2 = fun(a, b, c, i+1 , j, i+j+1,dp) +1 ;

            return dp[i][j]=min(op1,op2) ;

        }

        else {

            int op1 = fun(a, b, c, i+1, j , i+j+1,dp) ;

            int op2 = fun(a,b, c, i, j+1, i+j+1 ,dp);

            return dp[i][j]=min(op1,op2)+1 ;

        }

        return 0 ;

    }

    int lcs(string& a, string&b , int i , int j ){

        if(i>=a.size())return 0 ;

        if(j>=b.size() ) return 0 ;

        if(a[i]==b[j]){

            return 1+ lcs(a, b, i+1, j+1 ) ;

        }

        else {

            int op1 = lcs(a,b, i+1, j ) ;

            int op2 = lcs(a, b, i,j+1 ) ;

            return max(op1,op2) ;

        }

        return 0 ;

    }

    void solve()

    {

      string a , b,c ;

      cin>>a>>b>>c ;

      int n = a.size() ;

      int m= b.size() ;

      vector<vector<int>>dp(n, vector<int>(m,-1));

      int ans = fun(a,b, c, 0 , 0 , 0, dp) ;

      cout<<ans<<"\n" ;

    //   int res = -2 ;

    //   for(int i=0 ; i<2*n+2; i++){

    //     res = max(res, dp[n-1][i]);

    //   }

    //   cout<<res<<"\n";

      //print(dp);

    // int x =lcs(a, c , 0 , 0) ;

    // int y =lcs(b,c , 0 , 0 ) ;

    // cout<<(n+m)-(x+y)<<"\n";

    }

    signed main()

    {

    # ifndef ONLINE_JUDGE

    freopen("input.txt", "r", stdin);

    cout << "Reading from input.txt\n";

    freopen("output.txt", "w", stdout);

    if (!freopen("input.txt", "r", stdin)) {

        cerr << "Could not open input.txt\n";

        return 1;

    }

    #endif

        //fastio()  ;

        int  t;

    cin >> t;

    // t=1 ;

    for(int it=1;it<=t;it++) {

        // if(it==246){

        //      int n , m ;

        //      cin>>n>>m ;

        //      string s ="" ;

        //      for(int i =0 ; i<n ; i++){

        //         string a ;

        //         cin>>a ;

        //         s+=a ;

        //         s+='0';

        //      }

        //      vector<int>ans ;

        //      for(int i = 0 ; i<m ; i++){

        //             int  x ;

        //             cin>>x ;

        //             ans.push_back(x) ; xv

        //      }

        //      cout<<s<<" ";

        //      for(auto it : ans )cout<<it<<"\n";

        // }

        // else

        solve();

    }

    return 0;

    }

Answer: 2


#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const long long MOD = 1000000007;

#define all(c) (c).begin(), (c).end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2e5 + 10;
const ll INF = 1e18;

int solve(int i, int j, string &a, string &b, vector<vector<int>> &dp) {
    if (i == a.size() || j == b.size()) return 0;
    if (~dp[i][j]) return dp[i][j];
    int ret = 0;
    if (a[i] == b[j]) ret = max(ret, solve(i + 1, j + 1, a, b, dp) + 1);
    ret = max(ret, solve(i + 1, j, a, b, dp));
    ret = max(ret, solve(i, j + 1, a, b, dp));
    return dp[i][j]= ret;
}
vector<bool> vis(MAXN);
int cnt;
void print(int i, int j, string &a, string &b, vector<vector<int>>&dp) {
    if (i == a.size() || j == b.size()) return;
    int opt = solve(i, j, a, b, dp);
    if (a[i] == b[j] && opt == solve(i + 1, j + 1, a, b, dp) + 1) {
		vis[i] = true;
        return print(i + 1, j + 1, a, b, dp);
    }
    if (opt == solve(i + 1, j, a, b, dp)) return print(i + 1, j, a, b, dp);
    return print(i, j + 1, a, b, dp);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int tc;
    cin >> tc;
    while (tc--) {
        string a, b, c;
        cin >> a >> b >> c;
		int n = a.size(), m = b.size();
		ll dp[n + 1][m + 1];
		memset(dp, n + m + 1, sizeof(dp));
		dp[0][0] = 0;
		for(int i = 0; i <=n; i++){
			for(int j = 0; j <=m; j++){
				int k = i + j;
				if(i < n){
					ll cnt = c[k] != a[i];
					dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cnt);
				}
				if(j < m){
					ll cnt = c[k] != b[j];
					dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cnt);
				}
			}
		}
		cout << dp[n][m] <<endl;
	}

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const long long MOD = 1000000007;

#define all(c) (c).begin(), (c).end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 2e5 + 10;
const ll INF = 1e18;

int solve(int i, int j, string &a, string &b, vector<vector<int>> &dp) {
    if (i == a.size() || j == b.size()) return 0;
    if (~dp[i][j]) return dp[i][j];
    int ret = 0;
    if (a[i] == b[j]) ret = max(ret, solve(i + 1, j + 1, a, b, dp) + 1);
    ret = max(ret, solve(i + 1, j, a, b, dp));
    ret = max(ret, solve(i, j + 1, a, b, dp));
    return dp[i][j]= ret;
}
vector<bool> vis(MAXN);
int cnt;
void print(int i, int j, string &a, string &b, vector<vector<int>>&dp) {
    if (i == a.size() || j == b.size()) return;
    int opt = solve(i, j, a, b, dp);
    if (a[i] == b[j] && opt == solve(i + 1, j + 1, a, b, dp) + 1) {
		vis[i] = true;
        return print(i + 1, j + 1, a, b, dp);
    }
    if (opt == solve(i + 1, j, a, b, dp)) return print(i + 1, j, a, b, dp);
    return print(i, j + 1, a, b, dp);
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int tc;
    cin >> tc;
    while (tc--) {
        string a, b, c;
        cin >> a >> b >> c;
		int n = a.size(), m = b.size();
		vector dp(n + 1, vector<int>(m + 1, n + m + 1));
		dp[0][0] = 0;
		for(int i = 0; i <=n; i++){
			for(int j = 0; j <=m; j++){
				int k = i + j;
				if(i < n){
					int cnt = c[k] != a[i];
					dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cnt);
				}
				if(j < m){
					int cnt = c[k] != b[j];
					dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cnt);
				}
			}
		}
		cout << dp[n][m] <<endl;
	}

    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int dp[2001][2001];
int f(string &a,string &b,string &c,int i,int j){
    if(dp[i][j]!=-1)
    return dp[i][j];
    if(i==a.length()&&j==b.length())
    return dp[i][j]=0;
    if(i>a.length()||j>b.length())
    return 1e5;
    int min_one=INT_MAX;
    if(i!=a.length()&&a[i]==c[i+j])
    min_one=min(min_one,f(a,b,c,i+1,j));
    if(j!=b.length()&&b[j]==c[i+j])
    min_one=min(min_one,f(a,b,c,i,j+1));
    min_one=min(min_one,1+min(f(a,b,c,i+1,j),f(a,b,c,i,j+1)));
    return dp[i][j]=min_one;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        for(int i=0;i<2001;i++){
            for(int j=0;j<2001;j++){
                dp[i][j]=-1;
            }
        }
        cout<<f(a,b,c,0,0)<<endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int f(string &a,string &b,string &c,int i,int j){
    if(dp[i][j]!=-1)
    return dp[i][j];
    if(i==a.length()&&j==b.length())
    return dp[i][j]=0;
    if(i>a.length()||j>b.length())
    return 1e5;
    int min_one=INT_MAX;
    if(i!=a.length()&&a[i]==c[i+j])
    min_one=min(min_one,f(a,b,c,i+1,j));
    if(j!=b.length()&&b[j]==c[i+j])
    min_one=min(min_one,f(a,b,c,i,j+1));
    min_one=min(min_one,1+min(f(a,b,c,i+1,j),f(a,b,c,i,j+1)));
    return dp[i][j]=min_one;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        string a,b,c;
        cin>>a>>b>>c;
        for(int i=0;i<1001;i++){
            for(int j=0;j<1001;j++){
                dp[i][j]=-1;
            }
        }
    }
}

Answer: 1


#include <bits/stdc++.h>
#define MOD 1000000007
#define loop(i, a, b) for (int i = a; i < b; i++)
#define append push_back
#define vi vector<int>
#define mi map<int, int>
#define sortv(a) sort(a.begin(), a.end())
#define int long long

typedef unsigned long long ull;

using namespace std;

void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

// <-----------SidZRed-------------->
// <----Why are you reading this---->

signed main() {
    fast_io();

    int t;
    cin >> t;

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

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

        // DP table: dp[x][i] -> min changes for first x chars of c using first i chars of a
        vector<vector<int>> dp(k + 1, vector<int>(n + 1, INT_MAX));
        dp[0][0] = 0;

        for (int x = 1; x <= k; x++) {
            if (x <= m) {
                dp[x][0] = dp[x - 1][0] + (c[x - 1] != b[x - 1]);
            }
        }

        for (int i = 1; i <= n; i++) {
            if (i <= k) {
                dp[i][i] = dp[i - 1][i - 1] + (c[i - 1] != a[i - 1]);
            }
        }

        for (int x = 1; x <= k; x++) {
            for (int i = 0; i <= min(x, n); i++) {
                int j = x - i;
                if (j > m) continue;

                if (j > 0 && x - 1 >= 0) {
                    dp[x][i] = min(dp[x][i], dp[x - 1][i] + (c[x - 1] != b[j - 1]));
                }

                if (i > 0 && x - 1 >= 0) {
                    dp[x][i] = min(dp[x][i], dp[x - 1][i - 1] + (c[x - 1] != a[i - 1]));
                }
            }
        }

        int ans = INT_MAX;
        for (int i = 0; i <= n; i++) {
            ans = min(ans, dp[k][i]);
        }

        cout << ans << endl;
    }
    return 0;
}

#include <bits/stdc++.h>

#define MOD 1000000007
#define loop(i,a,b) for(int i = a;i < b;i++)
#define append push_back
#define vi vector<int>
#define mi map<int, int>
#define sort(a) sort(a.begin(), a.end())
#define int long long

typedef unsigned long long ull;

using namespace std;

void fast_io() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
}

// <-----------SidZRed-------------->
// <----Why are you reading this---->

signed main(){
    fast_io();

    int t;
    cin>>t;

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

        vector<vector<int>> dp(c.length()+1, vector<int>(a.length() + 1, INT_MAX));
        // dp[x][i] represents the minimum number of changes needed to create the first x characters of c using the first i characters of a and first x-i characters of b.

        dp[0][0] = 0;

        for(int x=1;x<=c.length();x++){
            dp[x][0] = dp[x-1][0] + (c[x-1] != b[x-1]);
        }
        for(int i=1;i<=a.length();i++){
            dp[i][i] = dp[i-1][i-1] + (c[i-1] != a[i-1]);
        }

        for(int x=1;x<=c.length();x++){
            for(int i=1;i<=min(x, (int)a.length());i++){
                dp[x][i] = min(dp[x-1][i] + (c[x-1] != b[x-i-1]), dp[x-1][i-1] + (c[x-1] != a[i-1]));
            }
        }

        int ans = *min_element(dp[c.length()].begin(), dp[c.length()].end());
        cout<<ans<<endl;
    }
    return 0;
}

Answer: 1


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

#define ll long long
#define pb push_baci+j
#define ppb pop_baci+j
#define mp mai+je_pair
#define vi vector<int>
#define vll vector<long long>

#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()

#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)
#define fori(n) for (int i = 0; i < n; ++i)

#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define Yes { cout << "YES\n"; return; }
#define No { cout << "NO\n"; return; }

template<typename T> void print_value(const vector<T>& vec) { for (const auto& element : vec) cout << element << " "; }
template<typename T> void print_value(const T& value) { cout << value << " "; }
template<typename T, typename... Args> void print_value(const T& value, const Args&... args) { print_value(value); print_value(args...); }
template<typename T, typename... Args> void print(const T& value, const Args&... args) { print_value(value, args...); cout << endl; }

template<typename T> void debug_value(const vector<T>& vec) { cout << "[ "; for (const auto& element : vec) cout << element << " "; cout << "] "; }
template<typename T> void debug_value(const T& value) { cout << "# " << value << " "; }
template<typename T, typename... Args> void debug_value(const T& value, const Args&... args) { debug_value(value); debug_value(args...); }
template<typename T, typename... Args> void debug(const T& value, const Args&... args) { debug_value(value, args...); cout << endl; }

const int MOD = 1e9 + 7, INF = INT_MAX;

//------------------------Code Starts-------------------------

int rec(string a, string b, string c, int i, int j){

    if (i+j == c.size()) return 0;

    //come from a
    int sc1;

    if (c[i+j]==a[i]){
        sc1 = rec(a,b,c,i+1,j);
    }
    else{
        sc1 = 1 + rec(a,b,c,i+1,j);
    }

    //come from b
    int sc2;

    if (c[i+j]==b[j]){
        sc2 = rec(a,b,c,i,j+1);
    }
    else{
        sc2 = 1 + rec(a,b,c,i,j+1);
    }

    return min(sc1, sc2);

}

void solve(){
    string a,b,c; cin>>a>>b>>c;
    print(rec(a,b,c,0,0));
}

//------------------------Code Ends---------------------------

int main() {
    fastio;
    int T = 1;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

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

#define ll long long
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define vi vector<int>
#define vll vector<long long>

#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define all(v) v.begin(), v.end()

#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)
#define fori(n) for (int i = 0; i < n; ++i)

#define mxe(v) *max_element(v.begin(),v.end())
#define mne(v) *min_element(v.begin(),v.end())
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define Yes { cout << "YES\n"; return; }
#define No { cout << "NO\n"; return; }

template<typename T> void print_value(const vector<T>& vec) { for (const auto& element : vec) cout << element << " "; }
template<typename T> void print_value(const T& value) { cout << value << " "; }
template<typename T, typename... Args> void print_value(const T& value, const Args&... args) { print_value(value); print_value(args...); }
template<typename T, typename... Args> void print(const T& value, const Args&... args) { print_value(value, args...); cout << endl; }

const int MOD = 1e9 + 7, INF = INT_MAX;

//------------------------Code Starts-------------------------

int rec(string& a, string& b, string& c, int i, int j, vector<vector<int>>& dp) {
    if (i + j == c.size()) return 0;
    if (dp[i][j] != -1) return dp[i][j];

    int sc1 = INF, sc2 = INF;

    if (i < a.size() && c[i + j] == a[i]) {
        sc1 = rec(a, b, c, i + 1, j, dp);
    } else if (i < a.size()) {
        sc1 = 1 + rec(a, b, c, i + 1, j, dp);
    }

    if (j < b.size() && c[i + j] == b[j]) {
        sc2 = rec(a, b, c, i, j + 1, dp);
    } else if (j < b.size()) {
        sc2 = 1 + rec(a, b, c, i, j + 1, dp);
    }

    return dp[i][j] = min(sc1, sc2);
}

void solve() {
    string a, b, c;
    cin >> a >> b >> c;
    vector<vector<int>> dp(a.size() + 1, vector<int>(b.size() + 1, -1));
    print(rec(a, b, c, 0, 0, dp));
}

//------------------------Code Ends---------------------------

int main() {
    fastio;
    int T = 1;
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}

Answer: 2


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

#define int long long

void solve() {
	string a, b, c; cin >> a >> b >> c;
	int n = a.size(); int m = b.size();

	vector<vector<int>> dp(n+1, vector<int>(m+1, 1e9));
	dp[0][0] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= m; j++) {
			bool changeA = !(a[i-1] == c[i+j-1]);
			bool changeB = !(b[j-1] == c[i+j-1]);

			dp[i][j] = min(dp[i-1][j] + changeA, dp[i][j-1] + changeB);
		}
	}

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

signed main() {
	int t; cin >> t;

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

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

#define int long long

void solve() {
	string a, b, c; cin >> a >> b >> c;
	int n = a.size(); int m = b.size();

	vector<vector<int>> dp(n+1, vector<int>(m+1, 1e9));
	dp[0][0] = 0;
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= m; j++) {
			if (i < n) {
				int changeA = (a[i] != c[i+j]);
				dp[i+1][j] = min(dp[i+1][j], dp[i][j] + changeA);
			}

			if (j < m) {
				int changeB = (b[j] != c[i+j]);
				dp[i][j+1] = min(dp[i][j+1], dp[i][j] + changeB);
			}
		}
	}

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

signed main() {
	int t; cin >> t;

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

Answer: 2


//#include "dec24_1.h"

#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

using namespace std;

int SolveHelper(std::vector<std::vector<int>>& dp, std::string_view string1,
                std::string_view string2, std::string_view string3, int p1,
                int p2, int p3) {
  if (dp[p1][p2] != -1) return dp[p1][p2];
  if (p1 == string1.size()) {
    if (dp[p1][p2] != -1) return dp[p1][p2];
    int count = 0;
    for (int i = p2; i < string2.size(); i++) {
      if (string2[i] != string3[p1 + i]) {
        count++;
      }
    }
    dp[p1][p2] = count;
    return count;
  }

  if (p2 == string2.size()) {
    int count = 0;
    for (int i = p1; i < string1.size(); i++) {
      if (string1[i] != string3[p2 + i]) {
        count++;
      }
    }
    dp[p1][p2] = count;
    return count;
  }
  int x;
  if (string1[p1] == string3[p3]) {
    x = SolveHelper(dp, string1, string2, string3, p1 + 1, p2, p3 + 1);
  } else {
    x = SolveHelper(dp, string1, string2, string3, p1 + 1, p2, p3 + 1) + 1;
  }
  if (string2[p2] == string3[p3]) {
    x = std::min(x,
          SolveHelper(dp, string1, string2, string3, p1, p2 + 1, p3 + 1));
  } else {
    x = std::min(x,
          SolveHelper(dp, string1, string2, string3, p1, p2 + 1, p3 + 1) + 1);
  }
  dp[p1][p2] = x;
  return x;
}

class Problem {
 public:
  Problem() = default;
  int Solve(std::vector<std::vector<int>>& dp,
            std::string_view string1,
            std::string_view string2,
            std::string_view string3) {
    return SolveHelper(dp, string1, string2, string3, 0, 0, 0);
  }
};

int main() {
  int n;
  cin >> n;
  std::vector<int> output;
  for (int i = 0; i < n; i++) {
    std::string string1, string2, string3;
    Problem problem;
    cin >> string1;
    cin >> string2;
    cin >> string3;
    std::vector<std::vector<int>> matrix(string1.size() + 1,
                                          std::vector<int>(string2.size() + 1,
                                                           -1));
    output.push_back(problem.Solve(matrix, std::string_view(string1),
                                   std::string_view(string2),
                                   std::string_view(string3)));
  }
  for (int i = 0; i < n; i++) {
    cout << output[i] << endl;
  }

  return 0;
}

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int min_val = 1000000000;
void SolveHelper(std::string& string1, std::string& string2,
                std::string& string3, int p1, int p2, int p3, int current) {
    if (p3 == string3.size()) {
      if (current < min_val) min_val = current;
      return;
    }

    if (p1 == string1.size()) {
      if (string2[p2] == string3[p3]) {
        SolveHelper(string1, string2, string3, p1, p2 + 1, p3 + 1, current);
      } else {
        SolveHelper(string1, string2, string3, p1, p2 + 1, p3 + 1, current + 1);
      }
      return;
    }
    if (p2 == string2.size()) {
      if (string1[p1] == string3[p3]) {
        SolveHelper(string1, string2, string3, p1 + 1, p2, p3 + 1, current);
      } else {
        SolveHelper(string1, string2, string3, p1 + 1, p2, p3 + 1, current + 1);
      }
      return;
    }

    if (string1[p1] == string3[p3]) {
      SolveHelper(string1, string2, string3, p1 + 1, p2, p3 + 1, current);
    }
    if (string2[p2] == string3[p3]) {
      SolveHelper(string1, string2, string3, p1, p2 + 1, p3 + 1, current);
    }
    SolveHelper(string1, string2, string3, p1 + 1, p2, p3 + 1, current + 1);
    SolveHelper(string1, string2, string3, p1, p2 + 1, p3 + 1, current + 1);
}

class Problem {
 public:
  Problem() = default;
  int Solve(std::string& string1, std::string& string2, std::string& string3) {
    min_val = 1000000000;
    SolveHelper(string1, string2, string3, 0, 0, 0, 0);
    return min_val;
  }
};

int main() {
  /*
     std::string line;
    std::vector<std::string> tokens;

    for (int i = 0; i < 3; i++) {
        std::getline(std::cin, line);
        std::istringstream iss(line);
        std::string token;

        while (std::getline(iss, token, ' ')) {
            tokens.push_back(token);
        }

        for (const std::string& tok : tokens) {
            std::cout << stoi(tok) << std::endl;
        }
        tokens.clear(); // Clear tokens for the next line
    }
    return 0;
  */
  int n;
  cin >> n;
  std::vector<int> output;
  for (int i = 0; i < n; i++) {
    std::string string1, string2, string3;
    Problem problem;
    cin >> string1;
    cin >> string2;
    cin >> string3;
    output.push_back(problem.Solve(string1, string2, string3));
  }
  for (int i = 0; i < n; i++) {
    cout << output[i] << endl;
  }

  return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v5;
long long power(long long A, long long B)
{
    if (B == 0)
        return 1;

    long long res = power(A, B / 2);

    if (B % 2)
        return res * res * A;
    else
        return res * res;
}
bool is_prime(int n){
   if(n==1){
       return 0;
   }
   bool b = true;
   for(int i=2;i*i<=n;i++){
       if(n%i==0){
           b = false;
           break;
       }
   }
   return b;
}
 void primeFactors(ll n)
{
     while(n%2==0){
         v5.push_back(2);
          n=(n/2);
     }
       for (int i = 3; i*i <= n; i = i+2){
            while (n%i == 0){
                  v5.push_back(i);
                n = n/i;
               }
               }
         if (n > 2){
            v5.push_back(n);
         }
}
bool cmp(pair<ll,ll> &a, pair<ll,ll> &b){
     if(a.first!=b.first){
         if(a.first>b.first) return true;
          return false;
     }
     else{
           if(a.second<b.second) return true;
           return false;
     }
}
ll factorial(ll n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
void bin(ll n)
{
    for(int i=31;i>=0;i--){
         cout<<((n>>i)&1);
    }
    cout<<endl;

}
ll maxSubArraySum(ll a[], ll size)
{
    ll max_so_far = LONG_LONG_MIN, max_ending_here = 0;

    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

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

             return  dp[i][j]=ans;

  }

  int main(){
ios_base::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
      string a,b,c;
      cin>>a>>b>>c;
      int n=a.size();
      int m=b.size();
      vector<vector<int>>dp(n,vector<int>(m,-1));
      cout<<f(0,0,n,m,a,b,c,dp)<<endl;

  }

  }

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<ll>v5;
long long power(long long A, long long B)
{
    if (B == 0)
        return 1;

    long long res = power(A, B / 2);

    if (B % 2)
        return res * res * A;
    else
        return res * res;
}
bool is_prime(int n){
   if(n==1){
       return 0;
   }
   bool b = true;
   for(int i=2;i*i<=n;i++){
       if(n%i==0){
           b = false;
           break;
       }
   }
   return b;
}
 void primeFactors(ll n)
{
     while(n%2==0){
         v5.push_back(2);
          n=(n/2);
     }
       for (int i = 3; i*i <= n; i = i+2){
            while (n%i == 0){
                  v5.push_back(i);
                n = n/i;
               }
               }
         if (n > 2){
            v5.push_back(n);
         }
}
bool cmp(pair<ll,ll> &a, pair<ll,ll> &b){
     if(a.first!=b.first){
         if(a.first>b.first) return true;
          return false;
     }
     else{
           if(a.second<b.second) return true;
           return false;
     }
}
ll factorial(ll n)
{
    if (n == 0)
        return 1;
    return n * factorial(n - 1);
}
void bin(ll n)
{
    for(int i=31;i>=0;i--){
         cout<<((n>>i)&1);
    }
    cout<<endl;

}
ll maxSubArraySum(ll a[], ll size)
{
    ll max_so_far = LONG_LONG_MIN, max_ending_here = 0;

    for (int i = 0; i < size; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

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

             return  dp[i][j]=ans;

  }

  int main(){
ios_base::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
      string a,b,c;
      cin>>a>>b>>c;
      int n=a.size();
      int m=b.size();
      vector<vector<int>>dp(n+1,vector<int>(m+1,-1));
      cout<<f(0,0,n,m,a,b,c,dp)<<endl;

  }

  }

Answer: 2


#include<iostream>
#include<cstring>
#include<vector>
using namespace std;

void solve() {
    string a, b, c;
    cin >> a >> b >> c;
    int na = a.size();
    int nb = b.size();
    vector<vector<int>> dp(na + 1, vector<int>(nb + 1, na + nb + 1));
    for (int i = 0; i <= a.size();i++) {
        for (int j = 0; j <= b.size();j++) {
            dp[i][j] = 1e7;
        }
    }
    dp[0][0] = 0;
    for (int i = 0; i <= a.size();i++) {
        for (int j = 0; j <= b.size();j++) {
            if (i == 0 && j == 0) {
                dp[i][j] = 0;
            }
            else {
                if (i == 0) {
                    dp[i][j] = min(dp[i][j], dp[i][j - 1] + (b[j - 1] != c[j - 1]));
                }
                else if (j == 0) {
                    dp[i][j] = min(dp[i][j], dp[i - 1][j] + (c[i - 1] != a[i - 1]));
                }
                else {
                    dp[i][j] = min(dp[i][j], min(dp[i - 1][j] + (c[i + j - 1] != a[i - 1]), dp[i][j - 1] + (c[i + j - 1] != b[j - 1])));
                }
            }
        }
    }
    cout << dp[a.size()][b.size()] << '\n';
}
int main() {
    int te;
    cin >> te;
    while (te--) {
        solve();
    }
}

#include<iostream>
#include<cstring>
using namespace std;

void solve() {
    char* a = new char[1005];
    char* b = new char[1005];
    char* c = new char[1005];
    cin.getline(a, 1000, '\n');
    cin.getline(b, 1000, '\n');
    cin.getline(c, 1000, '\n');
    int** dp = new int*[strlen(a) + 1];
    for (int i = 0; i <= strlen(a);i++) {
        dp[i] = new int[strlen(b) + 1];
    }
    for (int i = 0; i <= strlen(a);i++) {
        for (int j = 0; j <= strlen(b);j++) {
            dp[i][j] = 1e7;
        }
    }
    dp[0][0] = 0;
    for (int i = 0; i <= strlen(a);i++) {
        for (int j = 0; j <= strlen(b);j++) {
            if (i == 0 && j == 0) {
                dp[i][j] = 0;
            }
            else {
                if (i == 0) {
                    dp[i][j] = min(dp[i][j], dp[i][j - 1] + (b[j - 1] != c[j - 1]));
                }
                else if (j == 0) {
                    dp[i][j] = min(dp[i][j], dp[i - 1][j] + (c[i - 1] != a[i - 1]));
                }
                else {
                    dp[i][j] = min(dp[i][j], min(dp[i - 1][j] + (c[i + j - 1] != a[i - 1]), dp[i][j - 1] + (c[i + j - 1] != b[j - 1])));
                }
            }
        }
    }
    cout << dp[strlen(a)][strlen(b)] << '\n';
    for (int i = 0; i <= strlen(a);i++) {
        delete[] dp[i];
        dp[i] = NULL;
    }
    delete[] dp;
    dp = NULL;
    delete[] a;
    a = NULL;
    delete[] b;
    b = NULL;
    delete[] c;
    c = NULL;
}
int main() {
    int te;
    cin >> te;
    cin.ignore();
    while (te--) {
        solve();
    }
}

Answer: 1


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int md= 998244353;
const int N=200005;
const int M=1<<13;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
string dir="RLDU";
vector<vector<int>>dp;
string a,b,c;
int n,m;
int rec(int i,int j)
{
    if(i>a.size()||j>b.size())return 1e9;

    if(i==a.size())
    {
        int rem=0;
        for(int k=j;k<b.size();k++)
            if(b[k]!=c[k+a.size()])rem++;
        return rem;
    }
    if(j==b.size())
    {
        int rem=0;
        for(int k=i;k<a.size();k++)
            if(a[k]!=c[k+b.size()])rem++;
        return rem;
    }
    int &ret=dp[i][j];
    if(~ret)return ret;
    ret=1e9;
    if(i+1<=a.size())ret=min(ret,rec(i+1,j)+(c[i+j]!=a[i]));
    if(j+1<=b.size())ret=min(ret,rec(i,j+1)+(c[i+j]!=b[j]));

    return ret;
}
signed main()
{
    //freopen("distinct.in","r",stdin);
   //freopen("out.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
    int t=1;cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        dp=vector<vector<int>>(a.size()+5,vector<int>(b.size()+5,-1));
        cout<<rec(0,0)<<'\n';
    }
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int md= 998244353;
const int N=200005;
const int M=1<<13;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
string dir="RLDU";
vector<vector<int>>dp;
string a,b,c;
int n,m;
int rec(int i,int j)
{
    if(i>a.size()||j>b.size())return 1e9;

    if(i==a.size())
    {
        int rem=0;
        for(int k=j;k<b.size();k++)
            if(b[k]!=c[j+a.size()])rem++;
        return rem;
    }
    if(j==b.size())
    {
        int rem=0;
        for(int k=i;k<a.size();k++)
            if(a[k]!=c[i+b.size()])rem++;
        return rem;
    }
    int &ret=dp[i][j];
    if(~ret)return ret;
    ret=1e9;
    if(i+1<=a.size())ret=min(ret,rec(i+1,j)+(c[i+j]!=a[i]));
    if(j+1<=b.size())ret=min(ret,rec(i,j+1)+(c[i+j]!=b[j]));

    return ret;
}
signed main()
{
    //freopen("distinct.in","r",stdin);
   //freopen("out.txt","w",stdout);
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
    int t=1;cin>>t;
    while(t--)
    {
        cin>>a>>b>>c;
        dp=vector<vector<int>>(a.size()+5,vector<int>(b.size()+5,-1));
        cout<<rec(0,0)<<'\n';
    }
}

Answer: 1


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

typedef long long int lli;
const int modd = 1e9 + 7;

#define int long long
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define bac(i, a, b) for (int i = a; i >= b; i--)
#define yes "YES"
#define no "NO"
#define nl " \n"
#define ak(x) cout << x << "\n";
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
template <class T>using max_heap = priority_queue<T>;
template <class T>using min_heap = priority_queue<T, vector<T>, greater<T>>;

int solve(int l,int m,int n,string &a,string &b,string &c,vector<vector<int>>&dp){
	if(n==0) return 0;
	if(l == 0){
		// cout<<"AK:"<<"M:"<<m<<" N:"<<n<<nl;
		int cnt=0;
		for(int i=0;i<n;i++){
			if(b[i] != c[i]) cnt++;
		}
		return cnt;
	}
	if(m == 0){
		// cout<<"AK:"<<"L:"<<l<<" N:"<<n<<nl;
		int cnt=0;
		for(int i=0;i<n;i++){
			if(a[i] != c[i]) cnt++;
		}
		return cnt;
	}

	if(dp[l][m] != -1) return dp[l][m];

	if(a[l-1] == c[n-1] && b[m-1] == c[n-1]){
		return dp[l][m] = min(solve(l-1,m,n-1,a,b,c,dp),solve(l,m-1,n-1,a,b,c,dp));
	}
	if(a[l-1] == c[n-1]){
		return dp[l][m] = min(solve(l-1,m,n-1,a,b,c,dp),1+solve(l,m-1,n-1,a,b,c,dp));
	}
	if(b[m-1] == c[n-1]){
		return dp[l][m] = min(solve(l,m-1,n-1,a,b,c,dp),1+solve(l-1,m,n-1,a,b,c,dp));
	}
	else {
		// not matches with anyone then skip it
		return dp[l][m] = 1 + min(solve(l-1,m,n-1,a,b,c,dp),solve(l,m-1,n-1,a,b,c,dp));
	}
}

void solve(){
	string a,b,c; cin>>a>>b>>c;

	int l=a.size();
	int m=b.size();
	int n=c.size();
	vector<vector<int>>dp(l+1,vector<int>(m+1,-1));
	cout<<solve(l,m,n,a,b,c,dp)<<nl;
}

int32_t main(){
	fastio();
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

	int t = 1;
	cin >> t;

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

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

typedef long long int lli;
const int modd = 1e9 + 7;

#define int long long
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define all(x) begin(x), end(x)
#define allr(x) rbegin(x), rend(x)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define bac(i, a, b) for (int i = a; i >= b; i--)
#define yes "YES"
#define no "NO"
#define nl " \n"
#define ak(x) cout << x << "\n";
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
template <class T>using max_heap = priority_queue<T>;
template <class T>using min_heap = priority_queue<T, vector<T>, greater<T>>;

void solve(){

}

int32_t main(){
	fastio();
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif

	int t = 1;
	cin >> t;

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

Answer: 1


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

const int MAX_N = (int) 1e3;
const int INF = (int) 1e9;
int dp[MAX_N][MAX_N];
string a, b, c;
int n, m;

int sol (int i = 0, int j = 0) {
    if (i == n and j == m) {
        return 0;
    }
    int& ans = dp[i][j];
    if (ans != -1) {
        return ans;
    }
    ans = INF;
    int k = i + j;
    if (i < n) {
        ans = min(ans, sol(i + 1, j) + (a[i] != c[k]));
    }
    if (j < m) {
        ans = min(ans, sol(i, j + 1) + (b[j] != c[k]));
    }
    return ans;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt;
    cin >> tt;
    while (tt--) {
        cin >> a >> b >> c;
        n = (int) a.size();
        m = (int) b.size();
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= m; ++j) {
                dp[i][j] = -1;
            }
        }
        cout << sol() << '\n';
    }
    return 0;
}

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

const int MAX_N = (int) 1e3;
const int INF = (int) 1e9;
int dp[MAX_N + 1][MAX_N + 1];
string a, b, c;
int n, m;

int sol (int i = 0, int j = 0) {
    if (i == n and j == m) {
        return 0;
    }
    int& ans = dp[i][j];
    if (ans != -1) {
        return ans;
    }
    ans = INF;
    int k = i + j;
    if (i < n) {
        ans = min(ans, sol(i + 1, j) + (a[i] != c[k]));
    }
    if (j < m) {
        ans = min(ans, sol(i, j + 1) + (b[j] != c[k]));
    }
    return ans;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int tt;
    cin >> tt;
    while (tt--) {
        cin >> a >> b >> c;
        n = (int) a.size();
        m = (int) b.size();
        for (int i = 0; i <= n; ++i) {
            for (int j = 0; j <= m; ++j) {
                dp[i][j] = -1;
            }
        }
        cout << sol() << '\n';
    }
    return 0;
}

Answer: 2


#include <climits>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("trapv")

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

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long long int ll;
typedef unsigned long long int ull;
typedef priority_queue<ll> mxhpl;
typedef priority_queue<int> mxhpi;
typedef priority_queue<ll, vector<ll>, greater<ll>> mnhpl;
typedef priority_queue<int, vector<int>, greater<int>> mnhpi;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define REP(i, start, end) for (ll i = start; i <= end; ++i)
#define RREP(i, end, start) for (ll i = end; i >= start; --i)
#define parr(a, n)           \
    rep(i, 0, n) {           \
        cout << a[i] << " "; \
    }                        \
    cout << "\n"
#define sarr(a, n) rep(i, 0, n) cin >> a[i]
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define mset(a, x) memset(a, x, sizeof(a))
#define ps(x, y) \
    fixed << setprecision(y) << x  // cout << ps(ans, decimal places);
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)

// DEBUGGING
void __print(int x) {
    cerr << x;
}
void __print(long x) {
    cerr << x;
}
void __print(long long x) {
    cerr << x;
}
void __print(unsigned x) {
    cerr << x;
}
void __print(unsigned long x) {
    cerr << x;
}
void __print(unsigned long long x) {
    cerr << x;
}
void __print(float x) {
    cerr << x;
}
void __print(double x) {
    cerr << x;
}
void __print(long double x) {
    cerr << x;
}
void __print(char x) {
    cerr << '\'' << x << '\'';
}
void __print(const char* x) {
    cerr << '\"' << x << '\"';
}
void __print(const string& x) {
    cerr << '\"' << x << '\"';
}
void __print(bool x) {
    cerr << (x ? "true" : "false");
}

template <typename T, typename V>
void __print(const pair<T, V>& x) {
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x) {
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() {
    cerr << "]\n";
}
template <typename T, typename... V>
void _print(T t, V... v) {
    __print(t);
    if (sizeof...(v)) cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

// Fast IO
void IO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
}

#define MOD 1000000007

// CODE
/*******************************************************/
#define MX 1003  // TODO:

ll an, bn, cn;
string a, b, c;

ll dp[MX][MX];

ll recFun(ll i = 0, ll j = 0, ll k = 0, ll miss = 0) {
    if (dp[i][j] != INT_MAX && miss >= dp[i][j]) return dp[i][j];
    if (i >= an) {
        while (j < bn) {
            if (b[j] != c[k]) miss++;
            j++;
            k++;
        }
        return dp[i][j] = miss;
    }
    if (j >= bn) {
        while (i < an) {
            if (a[i] != c[k]) miss++;
            i++;
            k++;
        }
        return dp[i][j] = miss;
    }
    if (k >= cn) {
        return INT_MAX;
    }
    ll inc1 = a[i] == c[k] ? 0 : 1;
    ll inc2 = b[j] == c[k] ? 0 : 1;
    ll ans1 = recFun(i + 1, j, k + 1, miss + inc1);
    ll ans2 = recFun(i, j + 1, k + 1, miss + inc2);
    ans1 = min(ans1, ans2);
    dp[i][j] = min(dp[i][j], ans1);
    return dp[i][j];
}

void solve() {
    cin >> a >> b >> c;
    an = sz(a);
    bn = sz(b);
    cn = sz(c);
    ll* begin = &dp[0][0];
    size_t size = sizeof(dp) / sizeof(**dp);

    fill_n(begin, size, INT_MAX);

    cout << recFun() << endl;
}

int main() {
    IO();
    int t = 1;

    cin >> t;

    while (t--) {
        solve();
    }
    return 0;
}

/*******************************************************/

#include <climits>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("trapv")

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

typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef long long int ll;
typedef unsigned long long int ull;
typedef priority_queue<ll> mxhpl;
typedef priority_queue<int> mxhpi;
typedef priority_queue<ll, vector<ll>, greater<ll>> mnhpl;
typedef priority_queue<int, vector<int>, greater<int>> mnhpi;

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define REP(i, start, end) for (ll i = start; i <= end; ++i)
#define RREP(i, end, start) for (ll i = end; i >= start; --i)
#define parr(a, n)           \
    rep(i, 0, n) {           \
        cout << a[i] << " "; \
    }                        \
    cout << "\n"
#define sarr(a, n) rep(i, 0, n) cin >> a[i]
#define pb push_back
#define F first
#define S second
#define all(a) a.begin(), a.end()
#define mset(a, x) memset(a, x, sizeof(a))
#define ps(x, y) \
    fixed << setprecision(y) << x  // cout << ps(ans, decimal places);
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)

// DEBUGGING
void __print(int x) {
    cerr << x;
}
void __print(long x) {
    cerr << x;
}
void __print(long long x) {
    cerr << x;
}
void __print(unsigned x) {
    cerr << x;
}
void __print(unsigned long x) {
    cerr << x;
}
void __print(unsigned long long x) {
    cerr << x;
}
void __print(float x) {
    cerr << x;
}
void __print(double x) {
    cerr << x;
}
void __print(long double x) {
    cerr << x;
}
void __print(char x) {
    cerr << '\'' << x << '\'';
}
void __print(const char* x) {
    cerr << '\"' << x << '\"';
}
void __print(const string& x) {
    cerr << '\"' << x << '\"';
}
void __print(bool x) {
    cerr << (x ? "true" : "false");
}

template <typename T, typename V>
void __print(const pair<T, V>& x) {
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}
template <typename T>
void __print(const T& x) {
    int f = 0;
    cerr << '{';
    for (auto& i : x)
        cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}
void _print() {
    cerr << "]\n";
}
template <typename T, typename... V>
void _print(T t, V... v) {
    __print(t);
    if (sizeof...(v)) cerr << ", ";
    _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)               \
    cerr << "[" << #x << "] = ["; \
    _print(x)
#else
#define debug(x...)
#endif

// Fast IO
void IO() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
}

#define MOD 1000000007

// CODE
/*******************************************************/
#define MX 1003  // TODO:

ll an, bn, cn;
string a, b, c;

ll dp[MX][MX];

ll recFun(ll i = 0, ll j = 0, ll k = 0) {
    if (dp[i][j] != -1) return dp[i][j];
    if (i >= an && j >= bn) return 0;
    ll inc1 = a[i] == c[k] ? 0 : 1;
    ll inc2 = b[j] == c[k] ? 0 : 1;
    ll res = MOD;
    if (i < an) {
        ll ans1 = recFun(i + 1, j, k + 1) + inc1;
        res = min(res, ans1);
    }
    if (j < bn) {
        ll ans2 = recFun(i, j + 1, k + 1) + inc2;
        res = min(res, ans2);
    }
    dp[i][j] = res;
    return dp[i][j];
}

void solve() {
    cin >> a >> b >> c;
    an = sz(a);
    bn = sz(b);
    cn = sz(c);
    ll* begin = &dp[0][0];
    size_t size = sizeof(dp) / sizeof(**dp);

    fill_n(begin, size, -1);

    cout << recFun() << endl;
}

int main() {
    IO();
    int t = 1;

    cin >> t;

    while (t--) {
        solve();
    }
    return 0;
}

/*******************************************************/

Answer: 2


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

string a, b, c;
char v[1010];
int t, lenc, minc = 1010;
int lena, lenb;

void dfs(int x, int i, int j)
{
    if (x > lenc - 1)
    {
        int cnt = 0;
        for (int k = 0; k < lenc; k++)
        {
            if (v[k] != c[k])
                cnt++;
        }
        if (cnt < minc)
            minc = cnt;
        return;
    }
    if (i >lena || j > lenb)
        return;

    if (i < lena)
    {
        v[x] = a[i];
        dfs(x + 1, i + 1, j);
    }
    if (j < lenb)
    {
        v[x] = b[j];
        dfs(x + 1, i, j + 1);
    }
}

int main()
{
    cin >> t;
    while (t--)
    {
        cin >> a >> b >> c;
        lena = a.size();
        lenb = b.size();
        lenc = c.size();
        dfs(0, 0, 0);
        cout << minc << endl;
        minc = 1010;
    }
}

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

template <typename T>
bool cmin(T &a, const T &b)
{
    return b < a ? a = b, 1 : 0;
}

template <typename T>
bool cmax(T &a, const T &b)
{
    return b > a ? a = b, 1 : 0;
}

void go()
{
    string a, b, c;
    cin >> a >> b >> c;
    int n = a.size();
    int m = b.size();
    vector<vector<int>> f(n + 1, vector<int>(m + 1, -1));
    f[0][0] = 0;
    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= m; j++)
        {
            if (f[i][j] == -1)
            {
                continue;
            }
            if (i + 1 <= n)
            {
                cmax(f[i + 1][j], f[i][j] + (a[i] == c[i + j] ? 1 : 0));
            }
            if (j + 1 <= m)
            {
                cmax(f[i][j + 1], f[i][j] + (b[j] == c[i + j] ? 1 : 0));
            }
        }
    }
    cout << n + m - f[n][m] << endl;
}

int main()
{
    int caseCnt;
    cin >> caseCnt;
    for (int i = 1; i <= caseCnt; i++)
        go();
    return 0;
}

Answer: 2


#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 ans ;
void work(int i , int j , int cost , int flag){
    int idx = i+j;
    ll loc = cost;
    if(flag){

        if(s[idx]!=a[i]) loc++;

        if(dp[i][j]<=loc) return;
        dp[i][j]=min(dp[i][j],loc);

    }
    else {
        if(s[idx]!=b[j]) loc++;
        if(dp[i][j]<=loc) return;
        dp[i][j]=min(dp[i][j],loc);
    }
    if(i<n){

        ll nxt = dp[i][j]+(s[idx+1]==a[i+1]? 0 : 1);

        if(nxt<dp[i+1][j]){

            work(i+1,j,loc,1);
        }
    }
    if(j<m){
        ll nxt = dp[i][j]+(s[idx+1]==b[j+1] ? 0 : 1);
      //  cout<<nxt<<" "<<i<<" "<<j<<" "<<idx<<" this: "<<endl;
        if(nxt<dp[i][j+1]){
            work(i,j+1,loc,0);
        }
    }
    if(i==n && j==m) {
        //cout<<"yes"<<endl;
        ans = min( ans,dp[n][m]);
    }
    return;

}

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

    work(0,0,0,1);
    cout<<ans<<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();
    }
}

#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

Answer: 2


#include<bits/stdc++.h>
using namespace std;
int helper(int i , int j, string &a, string &b , string &c,vector<vector<int>> &dp) {
    if(i + j >= c.size()){
        return  dp[i][j] = 0 ;
    }
    int temp1 =1;
    if(a[i]==c[i+j]){
        temp1--;
    }
    int temp2 = 1;
    if(b[j] == c[i+j]){
        temp2--;
    }
    if(i==a.size()){

        return dp[i][j] = temp2 + helper(i , j+1 , a , b,c,dp);
    }
    if(j==b.size()){

        return dp[i][j] = temp1 + helper(i+1 , j , a , b,c,dp);
    }
    return dp[i][j] = min(temp1 + helper(i+1 , j , a , b,c,dp) ,temp2 + helper(i , j+1 , a , b,c,dp) );
}
int main()
{

    int n;
    cin>>n;

    while(n--)
    {
        string a  , b , c;
        cin>>a>>b>>c;
        vector<vector<int>> dp(a.size() + 1, vector<int> (b.size()+1));
        cout<<helper(0,0,a,b,c,dp);
        cout<<"\n";

    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int helper(int i , int j, string &a, string &b , string &c,vector<vector<int>> &dp) {
    if(dp[i][j]!=-1){
        return dp[i][j];
    }
    if(i + j >= c.size()){
        return  dp[i][j] = 0 ;
    }
    int temp1 =1;
    if(a[i]==c[i+j]){
        temp1--;
    }
    int temp2 = 1;
    if(b[j] == c[i+j]){
        temp2--;
    }
    if(i==a.size()){

        return dp[i][j] = temp2 + helper(i , j+1 , a , b,c,dp);
    }
    if(j==b.size()){

        return dp[i][j] = temp1 + helper(i+1 , j , a , b,c,dp);
    }
    return dp[i][j] = min(temp1 + helper(i+1 , j , a , b,c,dp) ,temp2 + helper(i , j+1 , a , b,c,dp) );
}
int main()
{

    int n;
    cin>>n;

    while(n--)
    {
        string a  , b , c;
        cin>>a>>b>>c;
        vector<vector<int>> dp(a.size() + 1, vector<int> (b.size()+1 , -1));
        cout<<helper(0,0,a,b,c,dp);
        cout<<"\n";

    }
    return 0;
}

Answer: 2


import java.util.*;

public class Solution {
    static int dp[][];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while(t-->0){
            String a=sc.next();
            String b=sc.next();
            String c=sc.next();
            dp=new int[a.length()+1][b.length()+1];

            for(int i=0;i<dp.length;i++){
                Arrays.fill(dp[i],-1);
            }
            System.out.println(solve(a,b,c,0,0,0));

        }
    }
    public static int solve(String a,String b,String c,int i,int j,int k){
        if(i==a.length() || j==b.length()){
            return calc(a,b,c,i,j,k);
        }
        if(dp[i][j]!=-1) return dp[i][j];
        if(c.charAt(k)==a.charAt(i) && c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));

        }
        else if(c.charAt(k)==a.charAt(i) ){
            return dp[i][j]=Math.min(solve(a,b,c,i+1,j,k+1),1+solve(a,b,c,i,j+1,k+1));
        }
        else if(c.charAt(k)==b.charAt(j)){
            return dp[i][j]=Math.min(solve(a,b,c,i,j+1,k+1),1+solve(a,b,c,i+1,j,k+1));
        }
        else return dp[i][j]=1+Math.min(solve(a,b,c,i+1,j,k+1),solve(a,b,c,i,j+1,k+1));
    }

    public static int calc(String a,String b,String c,int i,int j,int k){
        int count=0;
        if(i==a.length()){
            while(j<b.length()){
                if(b.charAt(j)!=c.charAt(k)){
                    count++;
                }
                k++;
                j++;
            }

        }
        else{
            while(i<a.length()){
                if(a.charAt(i)!=c.charAt(k)){
                    count++;
                }
                i++;
                k++;
            }
        }
        return count;
    }
}

import java.util.*;

public class Solution {
    static int dp1[][];
    static int dp2[][];
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();

        while(t-->0){
            String a=sc.next();
            String b=sc.next();
            String c=sc.next();
           dp1=new int[a.length()+1][c.length()+1];
            dp2=new int[b.length()+1][c.length()+1];

            for(int i=0;i<=a.length();i++){
                Arrays.fill(dp1[i],Integer.MAX_VALUE);
            }
            for(int i=0;i<=b.length();i++){
                Arrays.fill(dp2[i],Integer.MAX_VALUE);
            }
            System.out.println(solve(a,b,c,0,0,0));

        }
    }
    public static int solve(String a,String b,String c,int i,int j,int k){
        if(i==a.length() || j==b.length()){
            return calc(a,b,c,i,j,k);
        }
        if(dp1[i][k]!=Integer.MAX_VALUE && dp2[j][k]!=Integer.MAX_VALUE){
            return Math.min(dp1[i][k],dp2[j][k]);
        }
        if(c.charAt(k)==a.charAt(i) && c.charAt(k)==b.charAt(j)){
            return Math.min(dp1[i][k]=solve(a,b,c,i+1,j,k+1),dp2[j][k]=solve(a,b,c,i,j+1,k+1));

        }
        else if(c.charAt(k)==a.charAt(i) ){
            return Math.min(dp1[i][k]=solve(a,b,c,i+1,j,k+1),dp2[j][k]=1+solve(a,b,c,i,j+1,k+1));
        }
        else if(c.charAt(k)==b.charAt(j)){
            return Math.min(dp2[j][k]=solve(a,b,c,i,j+1,k+1),dp1[i][k]=1+solve(a,b,c,i+1,j,k+1));
        }
        else return 1+Math.min(dp1[i][k]=solve(a,b,c,i+1,j,k+1),dp2[j][k]=solve(a,b,c,i,j+1,k+1));
    }

    public static int calc(String a,String b,String c,int i,int j,int k){
        int count=0;
        if(i==a.length()){
            while(j<b.length()){
                if(b.charAt(j)!=c.charAt(k)){
                   count++;
                }
                k++;
                j++;
            }

        }
        else{
            while(i<a.length()){
                if(a.charAt(i)!=c.charAt(k)){
                    count++;
                }
                i++;
                k++;
            }
        }
        return count;
    }
}

Answer: 1


#include <cstdio>
#include <string>
#include <vector>
char buf[2000 + 3], buf1[1000 + 3], buf2[1000 + 3];
struct triple {
    int ind1;
    int ind2;
    int count;
};
int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        scanf(" %s %s %s", buf1, buf2, buf);
        std::string str1(buf1), str2(buf2), str(buf);
        int n = str1.size(), m = str2.size();
        std::vector<std::vector<int>> dp(n + 5, std::vector<int> (m + 5));
        dp[0][0] = 0;
        for (int i = 0; i < n; i++) {
            dp[i + 1][0] = dp[i][0] + (str1[i] != str[i]);
        }
        for (int i = 0; i < m; i++) {
            dp[0][i + 1] = dp[0][i] + (str2[i] != str[i]);
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                dp[i][j] = std::min(dp[i - 1][j] + (str1[i - 1] != str[i + j - 1]),
                                    dp[i][j - 1] + (str2[j - 1] != str[i + j - 1]));
            }
        }
        printf("%d\n", dp[n][m]);
    }
}

#include <cstdio>
#include <string>
#include <queue>
char buf[2000 + 3], buf1[1000 + 3], buf2[1000 + 3];
struct triple {
    int ind1;
    int ind2;
    int count;
};
int main() {
    int t = 1;
    scanf("%d", &t);
    while (t--) {
        scanf(" %s %s %s", buf1, buf2, buf);
        std::string str1(buf1), str2(buf2), str(buf);
        std::queue<triple> q;
//        printf("\n\n%s %s %s\n\n", str1.c_str(), str2.c_str(), str.c_str());
//        if (str1.size() < str2.size()) std::swap(str1, str2);
        q.push({1, 0, int(str1[0] != str[0])});
        q.push({0, 1, int(str2[0] != str[0])});
        int index = 1;
        int ans = 1000'000'000;
        int counter = 1;
        while (!q.empty()) {
            triple &cur = q.front(); bool bl1 = 1, bl2 = 1;
            q.pop();
//            printf("%d.%d %d %d -> \n", counter++, cur.ind1, cur.ind2, cur.count);
            if (cur.ind1 < str1.size()) {
                q.push({cur.ind1 + 1, cur.ind2, cur.count + int(str1[cur.ind1] != str[cur.ind1 + cur.ind2])});
                bl1 = 0;
            }
            if (cur.ind2 < str2.size()) {
                q.push({cur.ind1, cur.ind2 + 1, cur.count + int(str2[cur.ind2] != str[cur.ind1 + cur.ind2])});
                bl2 = 0;
            }
            if (bl1 & bl2) {
                ans = std::min(ans, cur.count);
//                printf("END!!!");
            }
//            printf("\n");
        }
        printf("%d\n", ans);
    }
}

Answer: 1


/**
    @author: ddi4z
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

// integer vectors
typedef vector<int> vi;
typedef vector<ll> vll;

// vectors of vectors of integers
typedef vector<vi> vvi;
typedef vector<vll> vvll;

// pairs of integers
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

// vector of pairs of integers
typedef vector<pii> vpii;
typedef vector<pll> vpll;

// stack of integers
typedef stack<int> si;

// stack of pairs of integers
typedef stack<pii> spii;

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fori(i, s, n) for(int i = s; i < n; ++i)
#define forll(i, s, n) for(ll i = s; i < n; ++i)
#define rfori(i, s, n) for(int i = s; i >= n; --i)
#define rforll(i, s, n) for(ll i = s; i >= n; --i)

const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;

#define DBG(X) cerr << #X << " = " << X << endl;
#define RAYA cerr << " ========================= " << endl;

// order statistics

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;
//ordered_set order;

int mod(int a, int m) {return ((a%m) + m) % m;}

string a, b, c;

map<string, ll> memo;

ll dp(ll i,ll j){
    string s = to_string(i) + "," + to_string(j);
    if (memo.count(s)) return memo[s];
    if (i < a.size() && j == b.size()) return memo[s] = (a[i] != c[i + j]) + dp(i + 1, j);
    if (i == a.size() && j < b.size()) return memo[s] = (b[j] != c[i + j]) + dp(i, j + 1);
    if (i == a.size() && j == b.size()) return 0;
    ll res = min((a[i] != c[i + j]) + dp(i + 1, j),
                (b[j] != c[i + j]) + dp(i, j + 1));
    return memo[s] = res;
}

int main() {
    ll TT;
    scanf("%lld", &TT);
    while(TT--) {
        cin >> a >> b >> c;
        memo.clear();
        printf("%lld\n", dp(0,0));
    }
    return 0;
}

/**
    @author: ddi4z
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;

// integer vectors
typedef vector<int> vi;
typedef vector<ll> vll;

// vectors of vectors of integers
typedef vector<vi> vvi;
typedef vector<vll> vvll;

// pairs of integers
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;

// vector of pairs of integers
typedef vector<pii> vpii;
typedef vector<pll> vpll;

// stack of integers
typedef stack<int> si;

// stack of pairs of integers
typedef stack<pii> spii;

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fori(i, s, n) for(int i = s; i < n; ++i)
#define forll(i, s, n) for(ll i = s; i < n; ++i)
#define rfori(i, s, n) for(int i = s; i >= n; --i)
#define rforll(i, s, n) for(ll i = s; i >= n; --i)

const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;

#define DBG(X) cerr << #X << " = " << X << endl;
#define RAYA cerr << " ========================= " << endl;

// order statistics

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update> ordered_set;
//ordered_set order;

int mod(int a, int m) {return ((a%m) + m) % m;}

string a, b, c;
vvll memo;

ll dp(ll i,ll j){

    if (memo[i][j]!=INF) return memo[i][j];
    if (i < a.size() && j == b.size()) return memo[i][j] = (a[i] != c[i + j]) + dp(i + 1, j);
    if (i == a.size() && j < b.size()) return memo[i][j] = (b[j] != c[i + j]) + dp(i, j + 1);
    if (i == a.size() && j == b.size()) return 0;
    ll res = min((a[i] != c[i + j]) + dp(i + 1, j),
                (b[j] != c[i + j]) + dp(i, j + 1));
    return memo[i][j] = res;
}

int main() {
    ll TT;
    scanf("%lld", &TT);
    while(TT--) {
        cin >> a >> b >> c;
        memo.assign(a.size() + 1, vll(b.size() + 1, INF));

        printf("%lld\n", dp(0,0));
    }
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
#define int long long
using namespace std;
int dp[1005][1005];
string a,b,c;
int solve(int i,int j)
{
    int k=i+j;
    if(dp[i][j]==-1)
    {
        dp[i][j]=1e18;
        if(i<a.size())dp[i][j]=solve(i+1,j)+(a[i]!=c[k]);
        if(j<b.size())dp[i][j]=min(dp[i][j],solve(i,j+1)+(b[j]!=c[k]));
        if(dp[i][j]==1e18)dp[i][j]=0;
    }

    return dp[i][j];
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   int t;
   cin>>t;
   while(t--)
   {
       memset(dp,-1,sizeof(dp));
       cin>>a>>b>>c;
       cout<<solve(0,0)<<'\n';
   }
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
int dp[1000][1000];
string a,b,c;
int solve(int i,int j)
{
    int k=i+j;
    if(dp[i][j]==-1)
    {
        dp[i][j]=1e18;
        if(i<a.size())dp[i][j]=solve(i+1,j)+(a[i]!=c[k]);
        if(j<b.size())dp[i][j]=min(dp[i][j],solve(i,j+1)+(b[j]!=c[k]));
        if(dp[i][j]==1e18)dp[i][j]=0;
    }

    return dp[i][j];
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
   int t;
   cin>>t;
   while(t--)
   {
       memset(dp,-1,sizeof(dp));
       cin>>a>>b>>c;
       cout<<solve(0,0)<<'\n';
   }
}

Answer: 1


/// Bismillah
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define double long double
#define pii pair<int , int>
#define endl '\n'
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> indexed_set;
typedef tree<int , null_type , less_equal<int> , rb_tree_tag , tree_order_statistics_node_update> indexed_multiset;
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0)
#define vi vector<int>
#define vpii vector<pii>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)((x).size())
#define all(x) (x).begin() , (x).end()
#define rall(x) (x).rbegin() , (x).rend()
#define F first
#define S second
const int mod = 1e9 + 7;
const int N = 3e3 + 5;
const int INF = (int)2e18 + 123;

int dp[N][N];

void solve()
{
    string a , b , c;
    cin >> a >> b >> c;
    int n = sz(a) , m = sz(b);

    for(int i = 0; i < n + m; i++){
        for(int j = 0; j <= min(i + 1 , n); j++){ /// j chars of a
            dp[i + 1][j] = INT_MAX;
            if(i - j < m)
                dp[i + 1][j] = dp[i][j] + (b[i - j] != c[i]);
            if(j)
                dp[i + 1][j] = min(dp[i + 1][j] , dp[i][j - 1] + (a[j - 1] != c[i]));
            //cout << dp[i + 1][j] << " ";
        }
        //cout << '\n';
    }
    cout << dp[n + m][n] << '\n';
}

signed main(){
    _FastIO;
    int T = 1;
    cin >> T;
    while(T--){
        solve();
    }
    //system("pause");
}

/// Bismillah
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define double long double
#define pii pair<int , int>
#define endl '\n'
typedef tree<int , null_type , less<int> , rb_tree_tag , tree_order_statistics_node_update> indexed_set;
typedef tree<int , null_type , less_equal<int> , rb_tree_tag , tree_order_statistics_node_update> indexed_multiset;
#define _FastIO ios_base::sync_with_stdio(0); cin.tie(0)
#define vi vector<int>
#define vpii vector<pii>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)((x).size())
#define all(x) (x).begin() , (x).end()
#define rall(x) (x).rbegin() , (x).rend()
#define F first
#define S second
const int mod = 1e9 + 7;
const int N = 3e3 + 5;
const int INF = (int)2e18 + 123;

int dp[N][N];

void solve()
{
    string a , b , c;
    cin >> a >> b >> c;
    int n = sz(a) , m = sz(b);
    for(int i = 0; i <= n + m + 2; i++){
        for(int j = 0; j <= n + 2; j++){
            dp[i][j] = INT_MAX;
        }
    }
    dp[0][0] = 0;
    for(int i = 0; i < n + m; i++){
        for(int j = 0; j <= min(i + 1 , n); j++){ /// j chars of a
            if(i - j < m && j <= i)
                dp[i + 1][j] = min(dp[i + 1][j] , dp[i][j] + (b[i - j] != c[i]));
            if(j)
                dp[i + 1][j] = min(dp[i + 1][j] , dp[i][j - 1] + (a[j - 1] != c[i]));
            //cout << dp[i + 1][j] << " ";
        }
        //cout << '\n';
    }
    cout << dp[n + m][n] << '\n';
}

signed main(){
    _FastIO;
    int T = 1;
    cin >> T;
    while(T--){
        solve();
    }
    //system("pause");
}

Answer: 2