import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class MyClass {
    public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        while (t-- > 0)
            solve(in, out);
        out.close();
    }
    static long pow(long x,long n, long m) {
        if(n==0) return 1;
        else if(n%2==0) return pow((x*x)%m,n/2,m)%m;
        else return (x*pow((x*x)%m,n/2,m))%m;
    }

    static int gcd(int a,int b) {
        if(b==0) return a;
        else return gcd(b,a%b);
    }

    static int ans;
    static void findSum(int[][] a,int i, int j, int sum) {
        if(i>2 || j>2) return;
        findSum(a,i,j+1, a[i][j]+sum);
        findSum(a,i+1,j, a[i][j]+sum);
        if(i==2 && j==2) ans=Math.min(a[i][j]+sum, ans);
    }

    static char[] a,b,c;
    static int findMaxMatching(int i, int j, int k){
        if(k>=c.length) return 0;
        int possibilityA=0,possibilityB=0,possibilityC=0;
        if(i>=a.length){
            int total=0;
            for (int l = j; l < b.length; l++) {
                if(b[l]==c[k]){
                   total++;
                }
                k++;
            }
            return total;
        }
        if(j>=b.length){
            int total=0;
            for (int l = i; l < a.length; l++) {
                if(a[l]==c[k]){
                    total++;
                }
                k++;
            }
            return total;
        }
        if(a[i]==c[k]){
            possibilityA = 1 + findMaxMatching(i+1,j,k+1);
        }
        if(b[j]==c[k]){
            possibilityB = 1 + findMaxMatching(i,j+1,k+1);
        }
        return Math.max(possibilityA, Math.max(possibilityB, Math.max(findMaxMatching(i,j+1,k+1), findMaxMatching(i+1,j,k+1))));
    }
    static void solve(FastScanner in,PrintWriter out) {
        a=in.next().toCharArray();
        b=in.next().toCharArray();
        c=in.next().toCharArray();
        int max = findMaxMatching(0,0,0);
        out.println(c.length-max);
    }

    static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> {
        public U x;
        public V y;

        public pair(U x, V y) {
            this.x = x;
            this.y = y;
        }

        public int compareTo(pair<U, V> other) {
            int i = other.y.compareTo(y);
            if (i != 0) return i;
            return other.x.compareTo(x);
        }

        public String toString() {
            return x.toString() + " " + y.toString();
        }

        public boolean equals(Object obj) {
            if (this.getClass() != obj.getClass()) return false;
            pair<U, V> other = (pair<U, V>) obj;
            return x.equals(other.x) && y.equals(other.y);
        }

        public int hashCode() {
            return 31 * x.hashCode() + y.hashCode();
        }

    }

    static void sort(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }
    static void sortR(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l,Collections.reverseOrder());
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }
}

import java.io.*;
import java.lang.reflect.Array;
import java.util.*;

public class MyClass {
    public static void main(String[] args) {
        FastScanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);
        int t = in.nextInt();
        while (t-- > 0)
            solve(in, out);
        out.close();
    }
    static long pow(long x,long n, long m) {
        if(n==0) return 1;
        else if(n%2==0) return pow((x*x)%m,n/2,m)%m;
        else return (x*pow((x*x)%m,n/2,m))%m;
    }

    static int gcd(int a,int b) {
        if(b==0) return a;
        else return gcd(b,a%b);
    }

    static int ans;
    static void findSum(int[][] a,int i, int j, int sum) {
        if(i>2 || j>2) return;
        findSum(a,i,j+1, a[i][j]+sum);
        findSum(a,i+1,j, a[i][j]+sum);
        if(i==2 && j==2) ans=Math.min(a[i][j]+sum, ans);
    }

    static char[] a,b,c;
    static int A[][];
    static int findMaxMatching(int i, int j, int k){
        if(i>=a.length){
            int total=0;
            for (int l = j; l < b.length; l++) {
                if(b[l]==c[k]){
                   total++;
                }
                k++;
            }
            return total;
        }
        if(j>=b.length){
            int total=0;
            for (int l = i; l < a.length; l++) {
                if(a[l]==c[k]){
                    total++;
                }
                k++;
            }
            return total;
        }
        if(A[i][j]>0) return A[i][j];
        int possibilityA = findMaxMatching(i+1,j,k+1);
        if(a[i]==c[k]){
            possibilityA++;
        }
        int possibilityB = findMaxMatching(i,j+1,k+1);
        if(b[j]==c[k]){
            possibilityB++;
        }
        A[i][j] = Math.max(possibilityA, possibilityB);
        return A[i][j];
    }
    static void solve(FastScanner in,PrintWriter out) {
        a=in.next().toCharArray();
        b=in.next().toCharArray();
        c=in.next().toCharArray();
        A=new int[a.length][b.length];
        int max = findMaxMatching(0,0,0);
        out.println(c.length-max);
    }

    static class pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<pair<U, V>> {
        public U x;
        public V y;

        public pair(U x, V y) {
            this.x = x;
            this.y = y;
        }

        public int compareTo(pair<U, V> other) {
            int i = other.y.compareTo(y);
            if (i != 0) return i;
            return other.x.compareTo(x);
        }

        public String toString() {
            return x.toString() + " " + y.toString();
        }

        public boolean equals(Object obj) {
            if (this.getClass() != obj.getClass()) return false;
            pair<U, V> other = (pair<U, V>) obj;
            return x.equals(other.x) && y.equals(other.y);
        }

        public int hashCode() {
            return 31 * x.hashCode() + y.hashCode();
        }

    }

    static void sort(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l);
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }
    static void sortR(int[] a) {
        ArrayList<Integer> l=new ArrayList<>();
        for (int i:a) l.add(i);
        Collections.sort(l,Collections.reverseOrder());
        for (int i=0; i<a.length; i++) a[i]=l.get(i);
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }
}

Answer: 2


def min_changes_needed(t, test_cases):
    results = []

    for a, b, c in test_cases:
        n, m = len(a), len(b)
        dp = [[0] * (m + 1) for _ in range(n + 1)]

        for i in range(n + 1):
            for j in range(m + 1):
                c_index = i + j - 1
                if i == 0 and j == 0:
                    continue
                if c_index >= len(c):
                    continue
                if i > 0 and a[i - 1] == c[c_index]:
                    dp[i][j] = max(dp[i][j], dp[i - 1][j] + 1)
                if j > 0 and b[j - 1] == c[c_index]:
                    dp[i][j] = max(dp[i][j], dp[i][j - 1] + 1)
        max_matched = dp[n][m]
        changes_needed = (n + m) - max_matched
        results.append(changes_needed)
    return results
t = 7
test_cases = [
    ("a", "b", "cb"),
    ("ab", "cd", "acbd"),
    ("ab", "ba", "aabb"),
    ("xxx", "yyy", "xyxyxy"),
    ("a", "bcd", "decf"),
    ("codes", "horse", "codeforces"),
    ("egg", "annie", "egaegaeg")
]
outputs = min_changes_needed(t, test_cases)
for output in outputs:
    print(output)

def min_changes(a, b, c):
    n, m = len(a), len(b)
    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]
    dp[0][0] = 0

    for i in range(n + 1):
        for j in range(m + 1):
            if i > 0:
                dp[i][j] = min(dp[i][j], dp[i-1][j] + (a[i-1] != c[i + j - 1]))
            if j > 0:
                dp[i][j] = min(dp[i][j], dp[i][j-1] + (b[j-1] != c[i + j - 1]))

    return dp[n][m]

t = int(input())
for _ in range(t):
    a = input().strip()
    b = input().strip()
    c = input().strip()
    print(min_changes(a, b, c))

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define csb_int __builtin_popcount
#define csb_ll __builtin_popcountll
#define all(x) x.begin(),x.end()
ll f(ll i,ll j,string&a,string&b,string&c,vector<vector<ll>>&dp){
    if(i+j==c.size()-1)return 0;
    if(dp[i][j]!=-1)return dp[i][j];
    ll op=c.size();
    if(i+1 < a.size()){
        if(a[i+1]==c[i+1+j]){
            op=min(op,f(i+1,j,a,b,c,dp));
        }
        if(a[i+1]!=c[i+1+j]){
            op=min(op,1+f(i+1,j,a,b,c,dp));
        }
    }
    if(j+1 < b.size()){
        if(b[j+1]==c[i+j+1]){
            op=min(op,f(i,j+1,a,b,c,dp));
        }
        if(b[j+1]!=c[i+j+1]){
            op=min(op,1+f(i,j+1,a,b,c,dp));
        }
    }
    return dp[i][j]=op;
}
void ans(){
    string a,b,c;
    cin>>a>>b>>c;
    string a1=" ";
    a1+=a;
    string b1=" ";
    b1+=b;
    string c1=" ";
    c1+=c;
    ll n=a1.size(),m=b1.size();
    vector<vector<ll>>dp(n,vector<ll>(m,-1));
    cout<<f(0,0,a1,b1,c1,dp)<<endl;
}
int main(){
int t;
cin>>t;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);cout.tie(NULL);
while(t--){
ans();
}
return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define csb_int __builtin_popcount
#define csb_ll __builtin_popcountll
#define all(x) x.begin(),x.end()
ll f(ll i,ll j,ll k,string&a,string&b,string&c,vector<vector<vector<ll>>>&dp){
    if(k==c.size())return 0;
    if(dp[i][j][k]!=-1)return dp[i][j][k];
    ll op=c.size();
    if(i<a.size() && a[i]==c[k] && j<b.size() && b[j]==c[k]){
        op= min(op,min(f(i+1,j,k+1,a,b,c,dp),f(i,j+1,k+1,a,b,c,dp)));
    }
    else if(i<a.size() && a[i]==c[k] && j<b.size() && b[j]!=c[k]){
        op=min(op,f(i+1,j,k+1,a,b,c,dp));
        op=min(op,1+f(i,j+1,k+1,a,b,c,dp));
    }
    else if(i<a.size() && a[i]!=c[k] && j<b.size() && b[j]==c[k]){
        op=min(op,1+f(i+1,j,k+1,a,b,c,dp));
        op=min(op,f(i,j+1,k+1,a,b,c,dp));
    }
    else if(i<a.size() && a[i]!=c[k] && j<b.size() && b[j]!=c[k]){
        op=min(op,1+f(i+1,j,k+1,a,b,c,dp));
        op=min(op,1+f(i,j+1,k+1,a,b,c,dp));
    }
    else{
        if(i<a.size()){
            if(a[i]==c[k]){
                op=min(op,f(i+1,j,k+1,a,b,c,dp));
            }
            else{
                op=min(op,1+f(i+1,j,k+1,a,b,c,dp));
            }
        }
        if(j<b.size()){
            if(b[j]==c[k]){
                op=min(op,f(i,j+1,k+1,a,b,c,dp));
            }
            else{
                op=min(op,1+f(i,j+1,k+1,a,b,c,dp));
            }
        }
    }
    return dp[i][j][k]=op;
}
void ans(){
    string a,b,c;
    cin>>a>>b>>c;
    ll l=a.size();
    ll n=b.size();
    ll m=c.size();
    vector<vector<vector<ll>>>dp(l+1,vector<vector<ll>>(n+1,vector<ll>(m+1,0)));
    for(ll i=l;i>=0;i--){
        for(ll j=n;j>=0;j--){
            for(ll k=m-1;k>=0;k--){
                ll op=c.size();
                if(i<a.size() && a[i]==c[k] && j<b.size() && b[j]==c[k]){
                    op= min(op,min(dp[i+1][j][k+1],dp[i][j+1][k+1]));
                }
                else if(i<a.size() && a[i]==c[k] && j<b.size() && b[j]!=c[k]){
                    op=min(op,dp[i+1][j][k+1]);
                    op=min(op,1+dp[i][j+1][k+1]);
                }
                else if(i<a.size() && a[i]!=c[k] && j<b.size() && b[j]==c[k]){
                    op=min(op,1+dp[i+1][j][k+1]);
                    op=min(op,dp[i][j+1][k+1]);
                }
                else if(i<a.size() && a[i]!=c[k] && j<b.size() && b[j]!=c[k]){
                    op=min(op,1+dp[i+1][j][k+1]);
                    op=min(op,1+dp[i][j+1][k+1]);
                }
                else{
                    if(i<a.size()){
                        if(a[i]==c[k]){
                            op=min(op,dp[i+1][j][k+1]);
                        }
                        else{
                            op=min(op,1+dp[i+1][j][k+1]);
                        }
                    }
                    if(j<b.size()){
                        if(b[j]==c[k]){
                            op=min(op,dp[i][j+1][k+1]);
                        }
                        else{
                            op=min(op,1+dp[i][j+1][k+1]);
                        }
                    }
                }
                dp[i][j][k]=op;
            }
        }
    }
    cout<<dp[0][0][0]<<endl;
    // cout<<f(0,0,0,a,b,c,dp)<<endl;
}
int main(){
int t;
cin>>t;
//ios_base::sync_with_stdio(false);
//cin.tie(NULL);cout.tie(NULL);
while(t--){
ans();
}
return 0;
}

Answer: 1


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

// Credits to HealthyUG for the inspiration.
// Segment Tree with Point Updates and Range Queries
// Supports multiple Segment Trees with just a change in the Node and Update
// Very few changes required everytime

template<typename Node, typename Update>
struct SegTree {
	vector<Node> tree;
	vector<int> arr; // type may change
	int n;
	int s;
	SegTree(int a_len) { // change if type updated
		vector<int> a(a_len+5,0);
        arr=a;
		n = a_len+5;
		s = 1;
		while(s < 2 * n){
			s = s << 1;
		}
		tree.resize(s);
        for(int i=0;i<s;i++){
            tree[i]=Node();
        }
        //fill(all(tree), Node());
		build(0, n - 1, 1);
	}
	void build(int start, int end, int index)  // Never change this
	{
		if (start == end)	{
			tree[index] = Node(arr[start]);
			return;
		}
		int mid = (start + end) / 2;
		build(start, mid, 2 * index);
		build(mid + 1, end, 2 * index + 1);
		tree[index].merge(tree[2 * index], tree[2 * index + 1]);
	}
	void update(int start, int end, int index, int query_index, Update &u)  // Never Change this
	{
		if (start == end) {
			u.apply(tree[index]);
			return;
		}
		int mid = (start + end) / 2;
		if (mid >= query_index)
			update(start, mid, 2 * index, query_index, u);
		else
			update(mid + 1, end, 2 * index + 1, query_index, u);
		tree[index].merge(tree[2 * index], tree[2 * index + 1]);
	}
	Node query(int start, int end, int index, int left, int right) { // Never change this
		if (start > right || end < left)
			return Node();
		if (start >= left && end <= right)
			return tree[index];
		int mid = (start + end) / 2;
		Node l, r, ans;
		l = query(start, mid, 2 * index, left, right);
		r = query(mid + 1, end, 2 * index + 1, left, right);
		ans.merge(l, r);
		return ans;
	}
	void make_update(int index, int val) {  // pass in as many parameters as required
		Update new_update = Update(val); // may change
		update(0, n - 1, 1, index, new_update);
	}
	Node make_query(int left, int right) {
		return query(0, n - 1, 1, left, right);
	}
};

struct Node1 {
	long long val; // may change
	Node1() { // Identity element
		val = 0;	// may change
	}
	Node1(long long p1) {  // Actual Node
		val = p1; // may change
	}
	void merge(Node1 &l, Node1 &r) { // Merge two child nodes
		val = max(l.val , r.val);  // may change
	}
};

struct Update1 {
	long long val; // may change
	Update1(long long p1) { // Actual Update
		val = p1; // may change
	}
	void apply(Node1 &a) { // apply update to given node
		a.val = max(a.val,val); // may change
	}
};

//Union Find Algorithm Almost O(1) for reasonable size N. O(log*N) to be precise
struct UnionFind {
	int n;
	vector<int> rank;
	vector<int> parent;
	// store other info as required
	UnionFind(int n) {
		rank.resize(n);
		fill(rank.begin(), rank.end(), 0);
		parent.resize(n);
		for (int i = 0; i < n; i++) {
			parent[i] = i;
		}
	}
	int get(int a) {
		return parent[a] = (parent[a] == a ? a : get(parent[a]));
	}
	void merge(int a, int b) {
		a = get(a);
		b = get(b);
		if (a == b) {
			return;
		}
		if (rank[a] == rank[b]) {
			rank[a]++;
		}
		if (rank[a] > rank[b]) {
			parent[b] = a;
		} else {
			parent[a] = b;
		}
	}
};

void ask(string s){
	cout<<1<<" "<<s<<endl;
}

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

	int takea=INT_MAX;
	int takeb=INT_MAX;
	takea=(a[i]!=c[i+j])+solve(a,b,c,i+1,j,n,m,dp);
	takeb=(b[j]!=c[i+j])+solve(a,b,c,i,j+1,n,m,dp);
	return dp[i][j]=min(takea,takeb);
}

signed main(){
	//  x^y = x + y - 2(x&y);
	int t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b>>c;
		vector<vector<int>> dp(a.length()+2,vector<int>(b.length()+2,-1));
		int ans=solve(a,b,c,0,0,a.length(),b.length(),dp);
		cout<<ans<<endl;
	}
}

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

// Credits to HealthyUG for the inspiration.
// Segment Tree with Point Updates and Range Queries
// Supports multiple Segment Trees with just a change in the Node and Update
// Very few changes required everytime

template<typename Node, typename Update>
struct SegTree {
	vector<Node> tree;
	vector<int> arr; // type may change
	int n;
	int s;
	SegTree(int a_len) { // change if type updated
		vector<int> a(a_len+5,0);
        arr=a;
		n = a_len+5;
		s = 1;
		while(s < 2 * n){
			s = s << 1;
		}
		tree.resize(s);
        for(int i=0;i<s;i++){
            tree[i]=Node();
        }
        //fill(all(tree), Node());
		build(0, n - 1, 1);
	}
	void build(int start, int end, int index)  // Never change this
	{
		if (start == end)	{
			tree[index] = Node(arr[start]);
			return;
		}
		int mid = (start + end) / 2;
		build(start, mid, 2 * index);
		build(mid + 1, end, 2 * index + 1);
		tree[index].merge(tree[2 * index], tree[2 * index + 1]);
	}
	void update(int start, int end, int index, int query_index, Update &u)  // Never Change this
	{
		if (start == end) {
			u.apply(tree[index]);
			return;
		}
		int mid = (start + end) / 2;
		if (mid >= query_index)
			update(start, mid, 2 * index, query_index, u);
		else
			update(mid + 1, end, 2 * index + 1, query_index, u);
		tree[index].merge(tree[2 * index], tree[2 * index + 1]);
	}
	Node query(int start, int end, int index, int left, int right) { // Never change this
		if (start > right || end < left)
			return Node();
		if (start >= left && end <= right)
			return tree[index];
		int mid = (start + end) / 2;
		Node l, r, ans;
		l = query(start, mid, 2 * index, left, right);
		r = query(mid + 1, end, 2 * index + 1, left, right);
		ans.merge(l, r);
		return ans;
	}
	void make_update(int index, int val) {  // pass in as many parameters as required
		Update new_update = Update(val); // may change
		update(0, n - 1, 1, index, new_update);
	}
	Node make_query(int left, int right) {
		return query(0, n - 1, 1, left, right);
	}
};

struct Node1 {
	long long val; // may change
	Node1() { // Identity element
		val = 0;	// may change
	}
	Node1(long long p1) {  // Actual Node
		val = p1; // may change
	}
	void merge(Node1 &l, Node1 &r) { // Merge two child nodes
		val = max(l.val , r.val);  // may change
	}
};

struct Update1 {
	long long val; // may change
	Update1(long long p1) { // Actual Update
		val = p1; // may change
	}
	void apply(Node1 &a) { // apply update to given node
		a.val = max(a.val,val); // may change
	}
};

//Union Find Algorithm Almost O(1) for reasonable size N. O(log*N) to be precise
struct UnionFind {
	int n;
	vector<int> rank;
	vector<int> parent;
	// store other info as required
	UnionFind(int n) {
		rank.resize(n);
		fill(rank.begin(), rank.end(), 0);
		parent.resize(n);
		for (int i = 0; i < n; i++) {
			parent[i] = i;
		}
	}
	int get(int a) {
		return parent[a] = (parent[a] == a ? a : get(parent[a]));
	}
	void merge(int a, int b) {
		a = get(a);
		b = get(b);
		if (a == b) {
			return;
		}
		if (rank[a] == rank[b]) {
			rank[a]++;
		}
		if (rank[a] > rank[b]) {
			parent[b] = a;
		} else {
			parent[a] = b;
		}
	}
};

void ask(string s){
	cout<<1<<" "<<s<<endl;
}

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

	int takea=INT_MAX;
	int takeb=INT_MAX;
	takea=(a[i]!=c[i+j])+solve(a,b,c,i+1,j,n,m,dp);
	takeb=(b[j]!=c[i+j])+solve(a,b,c,i,j+1,n,m,dp);
	return dp[i][j]=min(takea,takeb);
}

signed main(){
	//  x^y = x + y - 2(x&y);
	int t;
	cin>>t;
	while(t--){
		string a,b,c;
		cin>>a>>b>>c;
		vector<vector<int>> dp(a.length()+2,vector<int>(b.length()+2,-1));
		int ans=solve(a,b,c,0,0,a.length(),b.length(),dp);
		cout<<ans<<endl;
	}
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string a,b,c;
vector<vector<int>> dp;
int fnc(int i, int j)
{
  if(i>=a.length() && j >= b.length())
  {
    return 0;
  }
  if(dp[i][j] != -1)
  {
    return dp[i][j];
  }
  int x1 = 1e9;
  int x2 = 1e9;
  if(i < a.length())
  {
  x1 = fnc(i+1,j) + (a[i] != c[i+j]);
  }
  if(j < b.length())
  {
  x2 = fnc(i,j+1) + (b[j] != c[i+j]);
  }
  return dp[i][j] =  min(x1,x2);
}
void solve()
{
  cin >> a >> b >> c;
vector<vector<int>> ddp(a.length()+1,vector<int> (b.length()+1,-1));
dp = ddp;
  cout << fnc(0,0) << endl;
}
int32_t main(){
    int tt;
   cin >> tt;
   while(tt>0){
   solve();
   --tt;
   }

  //solve();
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
string a,b,c;
vector<vector<int>> dp(1e3+1,vector<int> (1e3+1,-1));
int fnc(int i, int j)
{
  if(i>=a.length() && j >= b.length())
  {
    return 0;
  }
  if(dp[i][j] != -1)
  {
    return dp[i][j];
  }
  int x1 = 1e9;
  int x2 = 1e9;
  if(i < a.length())
  {
  x1 = fnc(i+1,j) + (a[i] != c[i+j]);
  }
  if(j < b.length())
  {
  x2 = fnc(i,j+1) + (b[j] != c[i+j]);
  }
  return dp[i][j] =  min(x1,x2);
}
void solve()
{
  cin >> a >> b >> c;
vector<vector<int>> ddp(1e3+1,vector<int> (1e3+1,-1));
dp = ddp;
  cout << fnc(0,0) << endl;
}
int32_t main(){
    int tt;
   cin >> tt;
   while(tt>0){
   solve();
   --tt;
   }

  //solve();
}

Answer: 1


/* Coding the path to follow.... */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define gcd(m, n) __gcd(m,n)
#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)

int f(int i, int j, int &m, int &n, string& a, string& b, string& c, vector<vector<int>>& dp){
    if(i == m && j == n) return 0;

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

    int minChanges = 1e9;
    int k = i+j;
    if(i < m){
        if(a[i] == c[k]) minChanges = min(minChanges, f(i+1, j, m, n, a, b, c, dp));
        else minChanges = min(minChanges, 1 + f(i+1, j, m, n, a, b, c, dp));
    }
    if(j < n){
        if(b[j] == c[k]) minChanges = min(minChanges, f(i, j+1, m, n, a, b, c, dp));
        else minChanges = min(minChanges, 1 + f(i, j+1, m, n, a, b, c, dp));
    }

    return dp[i][j] = minChanges;
}

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

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

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

    cout << f(0, 0, m, n, a, b, c, dp) << endl;
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

    ll test=1;
    cin>>test;
    while(test--)
    {
        solve();
    }
    return 0;
}

/* Coding the path to follow.... */
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define gcd(m, n) __gcd(m,n)
#define FOR(a, c) for (int(a) = 0; (a) < (c); (a)++)

int f(int i, int j, int k, int &m, int &n, string& a, string& b, string& c, vector<vector<vector<int>>>& dp){
    if(i == m && j == n) return 0;

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

    int minChanges = 1e9;
    if(i < m){
        if(a[i] == c[k]) minChanges = min(minChanges, f(i+1, j, k+1, m, n, a, b, c, dp));
        else minChanges = min(minChanges, 1 + f(i+1, j, k+1, m, n, a, b, c, dp));
    }
    if(j < n){
        if(b[j] == c[k]) minChanges = min(minChanges, f(i, j+1, k+1, m, n, a, b, c, dp));
        else minChanges = min(minChanges, 1 + f(i, j+1, k+1, m, n, a, b, c, dp));
    }

    return dp[i][j][k] = minChanges;
}

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

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

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

    cout << f(0, 0, 0, m, n, a, b, c, dp) << endl;
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

    ll test=1;
    cin>>test;
    while(test--)
    {
        solve();
    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define PB push_back
#define all(x) x.begin(), x.end()
#define fast ios::sync_with_stdio(false); cin.tie(0);
#define Q int tt; cin>>tt ; for(int qq=1; qq <= tt; qq++)
#define endl "\n"

//PBDS start
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>//sudhu less dile uniqe element rakhbe ,,less_equal dile duplicate value rakhbe
new_data_set;
//it=s.find_by_order(k) return the iterator of k'th elemet , counting from 0(k'th index itertor)
//int cnt=s.oeder_of_key(k); return the number of elements strictly less than k

//PBDS end

//Debug start
template<typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream& os, const T_container& v) { os << '{'; string sep; for (const T& x : v) os << sep << x, sep = ", "; return os << '}'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
//Debug End

const ll mod=998244353;
ll inf=1e18;

long long big_mod(long long a,long long b,long long m)//[(a^b)%m] here b is very large number almost 1e9
{
    if(b==0)return (1%m);
    long long x=big_mod(a,b/2,m);
    x=(x*x)%m;
    if(b%2==1)x=(x*a)%m;
    return x;
}

vector<vector<int>>dp;

int solve(int i,int j,string a, string b, string c)
{
  if(i>=a.size() && j>=b.size())return 0;
  if(dp[i][j]!=-1)return dp[i][j];
  int ans,ans1=1e7, ans2=1e7;
  if(i<a.size() )
  {
    int d=0;
    if(a[i]!=c[i+j])d=1;
    ans1=d+solve(i+1,j,a,b,c);

  }
  if(j<b.size())
  {
    int d=0;
    if(b[j]!=c[i+j])d=1;
    ans2=d+solve(i,j+1,a,b,c);

  }
  ans=min(ans1,ans2);
  dp[i][j]=ans;
  return dp[i][j];

}

int main()
{
  fast;
  Q
  {
    string a,b,c;
    cin>>a>>b>>c;
    int n=a.size(),m=b.size();
    dp.assign(n+1,vector<int>(m+1,-1));
    int ans= solve(0,0,a,b,c);
    cout<<ans<<endl;

  }
return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define PB push_back
#define all(x) x.begin(), x.end()
#define fast ios::sync_with_stdio(false); cin.tie(0);
#define Q int tt; cin>>tt ; for(int qq=1; qq <= tt; qq++)
#define endl "\n"

//PBDS start
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>//sudhu less dile uniqe element rakhbe ,,less_equal dile duplicate value rakhbe
new_data_set;
//it=s.find_by_order(k) return the iterator of k'th elemet , counting from 0(k'th index itertor)
//int cnt=s.oeder_of_key(k); return the number of elements strictly less than k

//PBDS end

//Debug start
template<typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream& os, const T_container& v) { os << '{'; string sep; for (const T& x : v) os << sep << x, sep = ", "; return os << '}'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
//Debug End

const ll mod=998244353;
ll inf=1e18;

long long big_mod(long long a,long long b,long long m)//[(a^b)%m] here b is very large number almost 1e9
{
    if(b==0)return (1%m);
    long long x=big_mod(a,b/2,m);
    x=(x*x)%m;
    if(b%2==1)x=(x*a)%m;
    return x;
}

vector<vector<int>>dp;

int solve(int i,int j,string &a, string &b, string &c)
{
  if(i>=a.size() && j>=b.size())return 0;
  if(dp[i][j]!=-1)return dp[i][j];
  int ans,ans1=1e7, ans2=1e7;
  if(i<a.size() )
  {
    int d=0;
    if(a[i]!=c[i+j])d=1;
    ans1=d+solve(i+1,j,a,b,c);

  }
  if(j<b.size())
  {
    int d=0;
    if(b[j]!=c[i+j])d=1;
    ans2=d+solve(i,j+1,a,b,c);

  }
  ans=min(ans1,ans2);
  dp[i][j]=ans;
  return dp[i][j];

}

int main()
{
  fast;
  Q
  {
    string a,b,c;
    cin>>a>>b>>c;
    int n=a.size(),m=b.size();
    dp.assign(n+1,vector<int>(m+1,-1));
    int ans= solve(0,0,a,b,c);
    cout<<ans<<endl;

  }
return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define Fast_IO                       \

    ios_base::sync_with_stdio(false); \

    cin.tie(NULL);                    \

    cout.tie(NULL);

#define int long long int

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

/* ***********  My code start here *********** */

int32_t main()

{

    Fast_IO

#ifndef ONLINE_JUDGE

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

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

#endif

    clock_t danger_close = clock();

    int t;

    cin >> t;

    while (t--)

    {

        string a = "", b = "", c = "";

        cin >> a >> b >> c;

        int x = a.size(), y = b.size();

        int dp[x+2][y+2];

        for(int i = 0; i <= x+1; i++)

            for(int j = 0; j <= y+1; j++)

                dp[i][j] = 0;

        for(int i = 1; i <= x; i++)

        {

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

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

            else

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

        }

        for(int i = 1; i <= y; i++)

        {

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

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

            else

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

        }

        for(int i = 1; i <= x; i++)

        {

            for(int j = 1; j <= y; j++)

            {

                int m1, m2;

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

                    m1 = dp[i-1][j];

                else

                    m1 = dp[i-1][j] + 1;

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

                    m2 = dp[i][j-1];

                else

                    m2 = dp[i][j-1] + 1;

                dp[i][j] = min(m1, m2);

            }

        }

        cout << dp[x][y] << endl;

    }

    debug("Total Time: %.3f\n", (double)(clock() - danger_close) / CLOCKS_PER_SEC);

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define Fast_IO                       \

    ios_base::sync_with_stdio(false); \

    cin.tie(NULL);                    \

    cout.tie(NULL);

#define int long long int

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

/* ***********  My code start here *********** */

int32_t main()

{

    Fast_IO

#ifndef ONLINE_JUDGE

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

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

#endif

    clock_t danger_close = clock();

    int t;

    cin >> t;

    while (t--)

    {

        string a = "", b = "", c = "";

        cin >> a >> b >> c;

        c = " " + c;

        a = " " + a;

        b = " " + b;

        int x = a.size(), y = b.size();

        int dp[x+2][y+2];

        for(int i = 0; i <= x+1; i++)

            for(int j = 0; j <= y+1; j++)

                dp[i][j] = 0;

        for(int i = 1; i <= x; i++)

        {

            if(a[i] == c[i])

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

            else

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

        }

        for(int i = 1; i <= y; i++)

        {

            if(b[i] == c[i])

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

            else

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

        }

        for(int i = 1; i <= x; i++)

        {

            for(int j = 1; j <= y; j++)

            {

                int m1, m2;

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

                    m1 = dp[i-1][j];

                else

                    m1 = dp[i-1][j] + 1;

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

                    m2 = dp[i][j-1];

                else

                    m2 = dp[i][j-1] + 1;

                dp[i][j] = min(m1, m2);

            }

        }

        if(dp[x][y] > 0)

            dp[x][y]--;

        cout << dp[x][y] << endl;

    }

    debug("Total Time: %.3f\n", (double)(clock() - danger_close) / CLOCKS_PER_SEC);

    return 0;

}

Answer: 1


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

int diffCount(string a, string b)
{
    int count = 0;
    for (int i = 0; i < a.size(); i++)
    {
        if (a[i] != b[i])
            count++;
    }
    return count;
}

int dp_helper(string &a, string &b, string &c, int i, int j, int k, vector<vector<int>> &arr)
{
    if(arr[i][j]!= -1){
        return arr[i][j];
    }
    if (i >= a.size() && j >= b.size())
    {
        return 0;
    }
    if (i >= a.size())
    {
        return arr[i][j] = diffCount(b.substr(j, b.size() - j), c.substr(k, c.size() - k));
    }
    else if (j >= b.size())
    {
        return arr[i][j] = diffCount(a.substr(i, a.size() - i), c.substr(k, c.size() - k));
    }
    if(a[i]==c[k] && b[j] == c[k]){
        return arr[i][j] =  min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }

    else if (a[i] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i + 1, j, k + 1, arr), 1 + dp_helper(a,b,c,i,j+1,k+1, arr));
    }
    else if (b[j] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i, j + 1, k + 1, arr), 1 + dp_helper(a, b, c, i + 1, j, k + 1, arr));
    }
    else
    {
        return arr[i][j] =  1 + min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }
}

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

        string a, b, c;
        cin >> a >> b >> c;
        vector<vector<int>> arr(1002, vector<int> (1002, -1));
        cout << dp_helper(a, b, c, 0, 0, 0, arr)<<endl;
    }
}

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

int diffCount(string a, string b)
{
    int count = 0;
    for (int i = 0; i < a.size(); i++)
    {
        if (a[i] != b[i])
            count++;
    }
    return count;
}

int dp_helper(string a, string b, string c, int i, int j, int k, vector<vector<int>> &arr)
{
    if(arr[i][j]!= -1){
        return arr[i][j];
    }
    if (i >= a.size() && j >= b.size())
    {
        return 0;
    }
    if (i >= a.size())
    {
        return arr[i][j] = diffCount(b.substr(j, b.size() - j), c.substr(k, c.size() - k));
    }
    else if (j >= b.size())
    {
        return arr[i][j] = diffCount(a.substr(i, a.size() - i), c.substr(k, c.size() - k));
    }
    if(a[i]==c[k] && b[j] == c[k]){
        return arr[i][j] =  min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }

    else if (a[i] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i + 1, j, k + 1, arr), 1 + dp_helper(a,b,c,i,j+1,k+1, arr));
    }
    else if (b[j] == c[k])
    {
        return arr[i][j] = min(dp_helper(a, b, c, i, j + 1, k + 1, arr), 1 + dp_helper(a, b, c, i + 1, j, k + 1, arr));
    }
    else
    {
        return arr[i][j] =  1 + min(dp_helper(a, b, c, i + 1, j, k + 1, arr), dp_helper(a, b, c, i, j + 1, k + 1, arr));
    }
}

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

        string a, b, c;
        cin >> a >> b >> c;
        vector<vector<int>> arr(1000, vector<int> (1000, -1));
        cout << dp_helper(a, b, c, 0, 0, 0, arr)<<endl;
    }
}

Answer: 1


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

using namespace std;

int minChanges(const string& a, const string& b, const string& c) {
    int n = a.size();
    int m = b.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));

    dp[0][0] = 0;

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

    for (int j = 0; j < m; j++) {
        dp[0][j + 1] = dp[0][j] + (b[j] != c[j]);
    }

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

    return dp[n][m];
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        cout << minChanges(a, b, c) << endl;
    }
    return 0;
}

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

using namespace std;

int minChanges(const string& a, const string& b, const string& c) {
    int n = a.size();
    int m = b.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));

    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= m; ++j) {
            int k = i + j;
            if (k == c.size()) break;

            if (i < n) {
                dp[i + 1][j] = dp[i][j] + (a[i] != c[k] ? 1 : 0);
            }

            if (j < m) {
                dp[i][j + 1] = dp[i][j] + (b[j] != c[k] ? 1 : 0);
            }
        }
    }

    return dp[n][m];
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        string a, b, c;
        cin >> a >> b >> c;
        cout << minChanges(a, b, c) << endl;
    }
    return 0;
}

Answer: 1


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

int minChange(int aIndex, int bIndex, string &a, string &b, string &c, vector<vector<int>> &dp) {
  if(aIndex + bIndex >= c.size())
    return 0;

  if(dp[aIndex][bIndex] != -1)
    return dp[aIndex][bIndex];

  int cIndex = aIndex + bIndex;
  int mn = c.size() - cIndex + 1;
  if(aIndex < a.size()) {
    if(a[aIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c, dp));
    mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c, dp) + 1);
  }
  if(bIndex < b.size()) {
    if(b[bIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c, dp));
    mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c, dp) + 1);
  }
  return dp[aIndex][bIndex] = mn;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);
  int t;
  cin >> t;
  while(t--) {
    string a, b, c;
    cin >> a >> b >> c;

    vector<vector<int>> dp(a.size() + 1, vector<int> (b.size() + 1, -1));
    cout << minChange(0, 0, a, b, c, dp) << endl;
  }
}

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

int minChange(int aIndex, int bIndex, string &a, string &b, string &c, vector<vector<int>> &dp) {
  if(aIndex + bIndex >= c.size())
    return 0;

  if(dp[aIndex][bIndex] != -1)
    dp[aIndex][bIndex];

  int cIndex = aIndex + bIndex;
  int mn = c.size() - cIndex + 1;
  if(aIndex < a.size()) {
    if(a[aIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c, dp));
    mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c, dp) + 1);
  }
  if(bIndex < b.size()) {
    if(b[bIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c, dp));
    mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c, dp) + 1);
  }
  return dp[aIndex][bIndex] = mn;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);
  int t;
  cin >> t;
  while(t--) {
    string a, b, c;
    cin >> a >> b >> c;

    vector<vector<int>> dp(a.size() + 1, vector<int> (b.size() + 1, -1));
    cout << minChange(0, 0, a, b, c, dp) << endl;
  }
}

Answer: 1


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

#define ll long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define vpii vector<pii>
#define F first
#define S second
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

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

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

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

    int k = i+j;

    if (i == a.size() && j == b.size())
        return 0;

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

    int left = INT_MAX;
    int right = INT_MAX;

    if(i<a.size() && a[i]==c[k]){
        left = solve(a,b,c,i+1,j,dp);
    }
    else if(i<a.size() && a[i]!=c[k]) {
        left = 1+solve(a,b,c,i+1,j,dp);
    }

    if(j<b.size() && b[j]==c[k]){
        right = solve(a,b,c,i,j+1,dp);
    }
    else if(j<b.size() && b[j]!=c[k]){
        right = 1+solve(a,b,c,i,j+1,dp);
    }

    return dp[i][j] = min(left,right);

}

void solve() {

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

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

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

}

int main() {
    fast_io;

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

    return 0;
}

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

#define ll long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define vi vector<int>
#define vll vector<long long>
#define vpii vector<pii>
#define F first
#define S second
#define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

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

int solve(string a,string b,string c,int i,int j,int k,vector<vector<vi>> &dp){

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

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

    int left = INT_MAX;
    int right = INT_MAX;

    if(i<a.size() && a[i]==c[k]){
        left = solve(a,b,c,i+1,j,k+1,dp);
    }
    else if(i<a.size() && a[i]!=c[k]) {
        left = 1+solve(a,b,c,i+1,j,k+1,dp);
    }

    if(j<b.size() && b[j]==c[k]){
        right = solve(a,b,c,i,j+1,k+1,dp);
    }
    else if(j<b.size() && b[j]!=c[k]){
        right = 1+solve(a,b,c,i,j+1,k+1,dp);
    }

    return dp[i][j][k] = min(left,right);

}

void solve() {

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

    vector<vector<vi>> dp(a.size()+1,vector<vi> (b.size()+1,vector<int> (c.size()+1,-1)));

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

}

int main() {
    fast_io;

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

    return 0;
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
typedef tuple<ll,ll,ll> tii;
const int mod = 1e9+7;
const int maxn = 1005;
const ll inf = 0x3f3f3f3f3f3f3f3f;

int dp[maxn][maxn];

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

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int t; cin >> t;
    while(t--)
        solve();
    return 0;
}

// "Pensa simples, joga leve, coda fofo."

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pii;
typedef tuple<ll,ll,ll> tii;
const int mod = 1e9+7;
const int maxn = 1005;
const ll inf = 0x3f3f3f3f3f3f3f3f;

int dp[maxn][maxn];

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

int main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    int t; cin >> t;
    while(t--)
        solve();
    return 0;
}

// "Pensa simples, joga leve, coda fofo."

Answer: 2


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

#define int long long
#define inf 1e9

signed main() {
    int t;
    cin >> t;
    while(t--) {
        string a, b, c;
        cin >> a >> b >> c;
        int lenA = a.size(), lenB = b.size();
        vector<vector<int>> dp(lenA + 1, vector<int>(lenB + 1, inf));
        dp[0][0] = 0;
        for (int i = 0; i <= lenA; ++i) {
            for (int j = 0; j <= lenB; ++j) {
                if (i < lenA) dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + (a[i] != c[i + j]));
                if (j < lenB) dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + (b[j] != c[i + j]));
            }
        }
        cout << dp[lenA][lenB] << endl;
    }
    return 0;
}

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

#define int long long

signed main(){
    int t;
    cin >> t;
    while(t--) {
        vector<int> fq(26, 0);
        string a, b, c;
        cin >> a >> b >> c;
        for(const auto& it : a) fq[it - 'a']++;
        for(const auto& it : b) fq[it - 'a']++;
        for(const auto& it : c) fq[it - 'a']--;
        int sum = 0;
        for(const auto& it : fq) sum += max(it, 0LL);
        cout << sum << endl;
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define ld long double

#define mp make_pair

#define F first

#define S second

#define pb push_back

#define upb upper_bound

#define lwb lower_bound

const int maxn = (1 << 10);

int n, m, dp[maxn][maxn];

string a, b, c;

void clear_array() {

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

        fill(dp[i], dp[i] + 3 + m, 0);

    }

    a.clear(), b.clear(), c.clear();

    n = m = 0;

}

void solve() {

    cin >> a >> b >> c;

    n = (int)(a.size());

    m = (int)(b.size());

    a = "#" + a; b = "#" + b; c = "#" + c;

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

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

            dp[i][j] = n + n + m + m;

        }

    }

    dp[0][0] = 0;

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

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

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

    }

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

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

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

    }

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

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

            int from_a = (a[i] == c[i + j] ? dp[i - 1][j] : dp[i - 1][j] + 1);

            int from_b = (b[j] == c[i + j] ? dp[i][j - 1] : dp[i][j - 1] + 1);

            dp[i][j] = min(from_a, from_b);

        }

    }

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

    clear_array();

}

void boost() {

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

    ios_base :: sync_with_stdio(false);

}

int main() {

    boost();

    int TT;

    cin >> TT;

    while (TT--) {

        solve();

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

vector<vector<int>>dp;

int fxn(string a,string b,string c,int i,int j)

{

    int n=c.length();

    if(i+j==n)return 0;

    //if(i==a.length()||j==b.length())return 1e9;

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

    int a1=1e9,b1=1e9;

    if(i<a.length()&&a[i]==c[i+j])

    {

        a1=fxn(a,b,c,i+1,j);

    }

    else if(i<a.length())

    {

         a1=1+fxn(a,b,c,i+1,j);

    }

     if(j<b.length()&&b[j]==c[i+j])

    {

        b1=fxn(a,b,c,i,j+1);

    }

    else if(j<b.length())

    {

         b1=1+fxn(a,b,c,i,j+1);

    }

    return dp[i][j]=min(a1,b1);

}

int main() {

    int t;

    cin>>t;

    while(t--){

         string a,b,c;

         cin>>a>>b>>c;

         //memset(dp,-1,sizeof(dp));

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

         int z=fxn(a,b,c,0,0);

         cout<<z<<endl;

    }

    return 0;

}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define ll long long

#define pii pair<ll, ll>

ll INF = 10000000000000000;

ll MOD = 1000000007;

const int N = 1 << 17;

const int inf = 100000000;

string a, b, c;

int as, bs, cs;

int dp[1002][1002];

int solve(int charc, int chara) {

    if(chara < 0 or chara > charc) return inf;

    if(charc == 0) return 0;

    if(dp[charc][chara] != -1) return dp[charc][chara];

    int ans = 0;

    int charb = charc - chara;

    int x, y;

    if(a[chara - 1] == c[charc - 1])

        x = solve(charc - 1, chara - 1);

    else

        x = 1 + solve(charc - 1, chara - 1);

    if(b[charb - 1] == c[charc - 1])

        y = solve(charc - 1, chara);

    else

        y = 1 + solve(charc - 1, chara);

    ans = min(x, y);

    return dp[charc][chara] = ans;

}

void solve() {

    int i, j;

    cin >> a >> b >> c;

    as = a.size(), bs = b.size(), cs = c.size();

    for(i = 0; i <= cs; i++) for(j = 0; j <= as; j++) dp[i][j] = -1;

    cout << solve(cs, as) << endl;

}

int main() {

    #ifdef LOCAL

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

    #endif

    std::ios::sync_with_stdio(false);

    // doo(); return 0;

    ll t = 1;

    cin >> t;

    while (t--) {

        solve();

    }

}

/*

1  1   0

2  4   2

3  9   6

*/

#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)

#define ll long long

#define pii pair<ll, ll>

ll INF = 10000000000000000;

ll MOD = 1000000007;

const int N = 1 << 17;

const int inf = 100000000;

string a, b, c;

int as, bs, cs;

int dp[2002][2002];

int solve(int charc, int chara) {

    if(chara < 0 or chara > charc) return inf;

    if(charc == 0) return 0;

    if(dp[charc][chara] != -1) return dp[charc][chara];

    int ans = 0;

    int charb = charc - chara;

    int x, y;

    if(a[chara - 1] == c[charc - 1])

        x = solve(charc - 1, chara - 1);

    else

        x = 1 + solve(charc - 1, chara - 1);

    if(b[charb - 1] == c[charc - 1])

        y = solve(charc - 1, chara);

    else

        y = 1 + solve(charc - 1, chara);

    ans = min(x, y);

    return dp[charc][chara] = ans;

}

void solve() {

    int i, j;

    cin >> a >> b >> c;

    as = a.size(), bs = b.size(), cs = c.size();

    for(i = 0; i <= cs; i++) for(j = 0; j <= as; j++) dp[i][j] = -1;

    cout << solve(cs, as) << endl;

}

int main() {

    #ifdef LOCAL

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

    #endif

    std::ios::sync_with_stdio(false);

    // doo(); return 0;

    ll t = 1;

    cin >> t;

    while (t--) {

        solve();

    }

}

/*

1  1   0

2  4   2

3  9   6

*/

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[2005];
    char* b = new char[2005];
    char* c = new char[2005];
    cin.getline(a, 2000, '\n');
    cin.getline(b, 2000, '\n');
    cin.getline(c, 2000, '\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>
using namespace std;
#define ll long long

// int dp[1001][1001];
// int valid[1001][1001];
// int t;

// int minChangedChars(string a, string b, string c)
// {
//   int i = a.size(), j = b.size();
//   if (valid[i][j] == t)
//     return dp[i][j];
//   valid[i][j] = t;
//   int acnt = 0, bcnt = 0;
//   if (a.size() == 0)
//   {
//     for (int i = 0; i < c.size(); i++)
//     {
//       if (c[i] != b[i])
//         bcnt++;
//     }
//     return dp[i][j] = bcnt;
//   }

//   if (b.size() == 0)
//   {
//     for (int i = 0; i < c.size(); i++)
//     {
//       if (c[i] != a[i])
//         acnt++;
//     }
//     return dp[i][j] = acnt;
//   }
//   acnt = minChangedChars(a.substr(1), b, c.substr(1));
//   if (a[0] != c[0])
//     acnt++;
//   bcnt = minChangedChars(a, b.substr(1), c.substr(1));
//   if (b[0] != c[0])
//     bcnt++;
//   return dp[i][j] = min(acnt, bcnt);
// }

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

int main()
{
  int t;
  cin >> t;
  while (t--)
  {
    string a, b, c;
    cin >> a >> b >> c;
    vector<vector<int>> dp(a.size() + 1, vector<int>(b.size() + 1, INT_MAX));
    dp[0][0] = 0;
    for (int i = 0; i <= a.size(); i++)
    {
      for (int j = 0; j <= b.size(); j++)
      {
        if (i > 0)
          dp[i][j] = min(dp[i][j], dp[i - 1][j] + (a[i - 1] != c[i + j - 1]));
        if (j > 0)
          dp[i][j] = min(dp[i][j], dp[i][j - 1] + (b[j - 1] != c[i + j - 1]));
      }
    }
    cout << dp[a.size()][b.size()] << endl;
    // cout << minChangedChars(a, b, c) << endl;
  }
  return 0;
}

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

int dp[1001][1001];

int minChangedChars(string a, string b, string c)
{
  int i = a.size(), j = b.size();
  if (dp[i][j] != -1)
    return dp[i][j];
  int acnt = 0, bcnt = 0;
  if (a.size() == 0)
  {
    for (int i = 0; i < c.size(); i++)
    {
      if (c[i] != b[i])
        bcnt++;
    }
    return dp[i][j] = bcnt;
  }

  if (b.size() == 0)
  {
    for (int i = 0; i < c.size(); i++)
    {
      if (c[i] != a[i])
        acnt++;
    }
    return dp[i][j] = acnt;
  }
  acnt = minChangedChars(a.substr(1), b, c.substr(1));
  if (a[0] != c[0])
    acnt++;
  bcnt = minChangedChars(a, b.substr(1), c.substr(1));
  if (b[0] != c[0])
    bcnt++;
  return dp[i][j] = min(acnt, bcnt);
}

int main()
{
  ll t;
  cin >> t;
  while (t--)
  {
    string a, b, c;
    cin >> a >> b >> c;
    for (int i = 0; i <= a.size(); i++)
      for (int j = 0; j <= b.size(); j++)
        dp[i][j] = -1;
    cout << minChangedChars(a, b, c) << endl;
  }
  return 0;
}

Answer: 1


for _ in[1]*int(input()):
 s,t,u=input(),input(),input();n,m=len(s),len(t)
 inf=10**9
 d=[[inf]*(m+1)for _ in range(n+1)]
 d[0][0]=0
 for i in range(n+1):
  for j in range(m+1):
   if i==j==0:continue
   k=i+j-1
   if k>=len(u):continue
   if i>0:
    c=d[i-1][j]+(s[i-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
   if j>0:
    c=d[i][j-1]+(t[j-1]!=u[k])
    if c<d[i][j]:d[i][j]=c
 print(d[n][m])

for _ in [0]*int(input()):
    x = input()
    y = input()
    z = input()
    n, m = len(x), len(y)
    from functools import lru_cache
    @lru_cache(None)
    def f(i, j):
        if i == j == 0:
            return 0
        k = i + j - 1
        if k >= len(z):
            return 1e9
        r = 1e9
        if i > 0:
            r = min(r, f(i - 1, j) + (x[i - 1] != z[k]))
        if j > 0:
            r = min(r, f(i, j - 1) + (y[j - 1] != z[k]))
        return r
    print(int(f(n, m)))

Answer: 1


//Jai Shree Ram

//Author : Meet Upadhyay

#include<bits/stdc++.h>

#include<unordered_set>

#define lli long long int

const lli MOD=1e9+7 ;

const lli MOD2= 998244353;

const lli inf=1e18;

bool sortbysec(const std::pair<int,int> &a,const std::pair<int,int> &b) { return (a.second < b.second); }

lli power(lli n,lli a){ lli res=1; while(a){ if(a%2) res*=n,a--; else   n*=n,a/=2;} return res;}

lli modpower(lli n,lli a,lli p){ lli res=1; while(a){ if(a%2) res= ((res*n)%p) ,a--; else n=((n*n)%p),a/=2;} return res;}

lli fastprime(lli a){ if (a != 2 && a % 2 == 0 || a < 2) return 0;  for(lli i = 3; i * i <= a; i += 2) if(a % i == 0) return 0; return 1;}

lli mod_mul_inverse(lli a,lli b) {return modpower(a , b-2, b);}

using namespace std;

template <unsigned int MOD>

struct ModInt {

using uint = unsigned int;

using ull = unsigned long long;

using M = ModInt;

uint v;

ModInt(lli _v = 0) { set_norm(_v % MOD + MOD); }

M& set_norm(uint _v) {  //[0, MOD * 2)->[0, MOD)

v = (_v < MOD) ? _v : _v - MOD;

return *this;

}

explicit operator bool() const { return v != 0; }

M operator+(const M& a) const { return M().set_norm(v + a.v); }

M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }

M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }

M operator/(const M& a) const { return *this * a.inv(); }

M& operator+=(const M& a) { return *this = *this + a; }

M& operator-=(const M& a) { return *this = *this - a; }

M& operator*=(const M& a) { return *this = *this * a; }

M& operator/=(const M& a) { return *this = *this / a; }

M operator-() const { return M() - *this; }

M& operator++(int) { return *this = *this + 1; }

M& operator--(int) { return *this = *this - 1; }

M pow(lli n) const {

if (n < 0) return inv().pow(-n);

M x = *this, res = 1;

while (n) {

if (n & 1) res *= x;

x *= x;

n >>= 1;

}

return res;

}

M inv() const {

lli a = v, b = MOD, p = 1, q = 0, t;

while (b != 0) {

t = a / b;

swap(a -= t * b, b);

swap(p -= t * q, q);

}

return M(p);

}

bool operator==(const M& a) const { return v == a.v; }

bool operator!=(const M& a) const { return v != a.v; }

friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }

static uint get_mod() { return MOD; }

};

;

using Mint = ModInt<1000000007>;

void solve()

{

    string a;

    cin>>a;

    string b;

    cin>>b;

    string c;

    cin>>c;

    lli n = a.size();

    lli m = b.size();

    vector<vector<lli>>dp(n+5, vector<lli>(m+5,1e14));

    dp[0][0] = 0;

    for(lli i=0;i<n;i++)

    {

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

    }

    for(lli i=0;i<n;i++)

    {

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

    }

    for(lli i=1;i<=n;i++)

    {

        for(lli j=1;j<=m;j++)

        {

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

        }

    }

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

}

int main()

{

    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t;

    cin>>t;

    while(t--)

    {

        solve();

    }

    return 0;

}

//Jai Shree Ram

//Author : Meet Upadhyay

#include<bits/stdc++.h>

#include<unordered_set>

#define lli long long int

const lli MOD=1e9+7 ;

const lli MOD2= 998244353;

const lli inf=1e18;

bool sortbysec(const std::pair<int,int> &a,const std::pair<int,int> &b) { return (a.second < b.second); }

lli power(lli n,lli a){ lli res=1; while(a){ if(a%2) res*=n,a--; else   n*=n,a/=2;} return res;}

lli modpower(lli n,lli a,lli p){ lli res=1; while(a){ if(a%2) res= ((res*n)%p) ,a--; else n=((n*n)%p),a/=2;} return res;}

lli fastprime(lli a){ if (a != 2 && a % 2 == 0 || a < 2) return 0;  for(lli i = 3; i * i <= a; i += 2) if(a % i == 0) return 0; return 1;}

lli mod_mul_inverse(lli a,lli b) {return modpower(a , b-2, b);}

using namespace std;

template <unsigned int MOD>

struct ModInt {

using uint = unsigned int;

using ull = unsigned long long;

using M = ModInt;

uint v;

ModInt(lli _v = 0) { set_norm(_v % MOD + MOD); }

M& set_norm(uint _v) {  //[0, MOD * 2)->[0, MOD)

v = (_v < MOD) ? _v : _v - MOD;

return *this;

}

explicit operator bool() const { return v != 0; }

M operator+(const M& a) const { return M().set_norm(v + a.v); }

M operator-(const M& a) const { return M().set_norm(v + MOD - a.v); }

M operator*(const M& a) const { return M().set_norm(ull(v) * a.v % MOD); }

M operator/(const M& a) const { return *this * a.inv(); }

M& operator+=(const M& a) { return *this = *this + a; }

M& operator-=(const M& a) { return *this = *this - a; }

M& operator*=(const M& a) { return *this = *this * a; }

M& operator/=(const M& a) { return *this = *this / a; }

M operator-() const { return M() - *this; }

M& operator++(int) { return *this = *this + 1; }

M& operator--(int) { return *this = *this - 1; }

M pow(lli n) const {

if (n < 0) return inv().pow(-n);

M x = *this, res = 1;

while (n) {

if (n & 1) res *= x;

x *= x;

n >>= 1;

}

return res;

}

M inv() const {

lli a = v, b = MOD, p = 1, q = 0, t;

while (b != 0) {

t = a / b;

swap(a -= t * b, b);

swap(p -= t * q, q);

}

return M(p);

}

bool operator==(const M& a) const { return v == a.v; }

bool operator!=(const M& a) const { return v != a.v; }

friend ostream& operator<<(ostream& os, const M& a) { return os << a.v; }

static uint get_mod() { return MOD; }

};

;

using Mint = ModInt<1000000007>;

void solve()

{

    string a;

    cin>>a;

    string b;

    cin>>b;

    string c;

    cin>>c;

    lli n = a.size();

    lli m = b.size();

    vector<vector<lli>>dp(n+5, vector<lli>(m+5,1e14));

    dp[0][0] = 0;

    for(lli i=0;i<n;i++)

    {

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

    }

    for(lli i=0;i<m;i++)

    {

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

    }

    for(lli i=1;i<=n;i++)

    {

        for(lli j=1;j<=m;j++)

        {

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

        }

    }

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

}

int main()

{

    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int t;

    cin>>t;

    while(t--)

    {

        solve();

    }

    return 0;

}

Answer: 2


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

int dp[1001][1001];

int minChange(int aIndex, int bIndex, string &a, string &b, string &c) {
  if(aIndex + bIndex >= c.size())
    return 0;

  if(dp[aIndex][bIndex] != -1)
    dp[aIndex][bIndex];

  int cIndex = aIndex + bIndex;
  int mn = c.size() - cIndex + 1;
  if(aIndex < a.size()) {
    if(a[aIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c));
    mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c) + 1);
  }
  if(bIndex < b.size()) {
    if(b[bIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c));
    mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c) + 1);
  }
  return dp[aIndex][bIndex] = mn;
}

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

    memset(dp, -1, sizeof dp);
    cout << minChange(0, 0, a, b, c) << endl;
  }
}

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

int minChange(int aIndex, int bIndex, string &a, string &b, string &c, vector<vector<int>> &dp) {
  if(aIndex + bIndex >= c.size())
    return 0;

  if(dp[aIndex][bIndex] != -1)
    return dp[aIndex][bIndex];

  int cIndex = aIndex + bIndex;
  int mn = c.size() - cIndex + 1;
  if(aIndex < a.size()) {
    if(a[aIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c, dp));
    mn = min(mn, minChange(aIndex + 1, bIndex, a, b, c, dp) + 1);
  }
  if(bIndex < b.size()) {
    if(b[bIndex] == c[cIndex])
      mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c, dp));
    mn = min(mn, minChange(aIndex, bIndex + 1, a, b, c, dp) + 1);
  }
  return dp[aIndex][bIndex] = mn;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);cout.tie(0);
  int t;
  cin >> t;
  while(t--) {
    string a, b, c;
    cin >> a >> b >> c;

    vector<vector<int>> dp(a.size() + 1, vector<int> (b.size() + 1, -1));
    cout << minChange(0, 0, a, b, c, dp) << endl;
  }
}

Answer: 2


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

int na, nb;
string a, b, c;

/*
7
a
b
cb
ab
cd
acbd
ab
ba
aabb
xxx
yyy
xyxyxy
a
bcd
decf
codes
horse
codeforces
egg
annie
egaegaeg
*/

int dfs(int p1, int p2, int i, vector<vector<vector<int>>>& vis)
{
    // dfs() ----- 在当前情形下 最少还需要变几个字符
    if (vis[p1][p2][i]) {
        return vis[p1][p2][i];
    }
    if (i == na + nb) {
        if (p1 < na) {
            return vis[p1][p2][i] = (a[p1] == c[i] ? 0 : 1);
        }
        else {
            return vis[p1][p2][i] = (b[p2] == c[i] ? 0 : 1);
        }
    }
    int d = 0x3f3f3f3f;
    if (p1 < na) {
        d = dfs(p1+1, p2, i+1, vis) + (a[p1] == c[i] ? 0 : 1);
    }
    if (p2 < nb) {
        d = min(d, dfs(p1, p2+1, i+1, vis) + (b[p2] == c[i] ? 0 : 1));
    }
    return vis[p1][p2][i] = d;
}

void solve()
{
    cin >> a >> b >> c;
    na = a.length(); nb = b.length();
    vector<vector<vector<int>>> vis(na + 1, vector<vector<int>>(nb + 1, vector<int>(na + nb + 1, 0)));
    cout << dfs(0, 0, 0, vis) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // freopen("in.txt", "r", stdin);

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

    system("pause");
    return 0;
}

#include <bits/stdc++.h>
#define MAX
using namespace std;
using ll = long long;
#define FIND(x) (vis.find(x) != vis.end())
#define KEY(p1, p2) ((p2)*(na+1) + (p1))
#define FINDKEY(p1, p2) (FIND(KEY((p1), (p2))))

int na, nb;
string a, b, c;

/*
7
a
b
cb
ab
cd
acbd
ab
ba
aabb
xxx
yyy
xyxyxy
a
bcd
decf
codes
horse
codeforces
egg
annie
egaegaeg
*/

int dfs(int p1, int p2, int i, unordered_map<int, int>& vis)
{
    // dfs() ----- 在当前情形下 最少还需要变几个字符
    // i一定等于p1+p2 记忆化不需要考虑i
    if (FINDKEY(p1, p2)) {
        return vis[KEY(p1, p2)];
    }
    if (i == na + nb - 1) {
        if (p1 < na) {
            return vis[KEY(p1, p2)] = (a[p1] == c[i] ? 0 : 1);
        }
        else {
            return vis[KEY(p1, p2)] = (b[p2] == c[i] ? 0 : 1);
        }
    }
    int d = 0x3f3f3f3f;
    if (p1 < na) {
        d = dfs(p1+1, p2, i+1, vis) + (a[p1] == c[i] ? 0 : 1);
    }
    if (p2 < nb) {
        d = min(d, dfs(p1, p2+1, i+1, vis) + (b[p2] == c[i] ? 0 : 1));
    }
    return vis[KEY(p1, p2)] = d;
}

void solve()
{
    cin >> a >> b >> c;
    na = a.length(); nb = b.length();
    // vector<vector<vector<int>>> vis(na + 1, vector<vector<int>>(nb + 1, vector<int>(na + nb + 1, 0)));
    unordered_map<int, int> vis;
    cout << dfs(0, 0, 0, vis) << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    // freopen("in.txt", "r", stdin);

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

    system("pause");
    return 0;
}

Answer: 2


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

vector<string> s;

void func(string &a, string &b, string &c, int inda, int indb) {
    if (inda == a.size() && indb == b.size()) {
        s.push_back(c);
        return;
    }

    if (inda < a.size()) {
        string m =  c + a[inda];
        func(a, b,m, inda + 1, indb);
    }

    if (indb < b.size()) {
        string n =  c + b[indb];
        func(a, b,n, inda, indb + 1);
    }
}

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

        s.clear();
        string m ="";
        func(a, b, m, 0, 0);

        // for (auto ele : s) {
        //     cout << ele << endl;
        // }
        int final_ct =10000000;
        for(auto ele :s){
            int ct = 0;
            for(int i = 0 ; i < ele.size(); ++i){
                if(c[i]!=ele[i])ct++;
            }
            final_ct = min(ct,final_ct);
        }
        cout<<final_ct<<endl;

    }
    return 0;
}

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

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

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

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

        // Base case
        dp[0][0] = 0;

        // Fill DP table
        for (int i = 0; i <= m; ++i) {
            for (int j = 0; j <= n; ++j) {
                if (i > 0) {
                    dp[i][j] = min(dp[i][j], dp[i-1][j] + (c[i+j-1] != a[i-1]));
                }
                if (j > 0) {
                    dp[i][j] = min(dp[i][j], dp[i][j-1] + (c[i+j-1] != b[j-1]));
                }
            }
        }

        // Output the result for this test case
        cout << dp[m][n] << endl;
    }

    return 0;
}

Answer: 2


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

void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    vector<pair<int, int>> ans;
    for (int i = 1; i < n; i++)
        for (int j = i + 1; j <= n; j++)
            ans.push_back({j, i});
    vector<bool> b(n + 1, false);
    for (int i = n - 1; ~i; i--){
        for (int j = vt[i] - 1; j >= 1; j--){
            if (!b[j])
                ans.push_back({j, vt[i]});
        }
        b[vt[i]] = true;
    }
    cout << ans.size() << '\n';
    for (auto &[x, y] : ans)
        cout << x << ' ' << y << '\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

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

void solve(){
    int n; cin >> n;
    vector<int> vt(n);
    for (int &i : vt) cin >> i;
    set<pair<int, int>> ans;
    // 1, 2, 3 -> 2, 1, 3
    vector<int> form(n);
    for (int i = 0; i < n; i++)
        form[i] = i + 1;
    for (int i = 0; i < n; i++){
        int idx = i;
        for (; idx < n; idx++)
            if (form[idx] == vt[i])
                break;
        if (idx + 1 < n){
            ans.insert({form[idx], form[idx + 1]});
            ans.insert({form[idx + 1], form[idx]});
        }
        for (int j = idx; j > i; j--){
            ans.insert({form[j], form[j - 1]});
            swap(form[j], form[j - 1]);
        }
    }
    cout << ans.size() << '\n';
    for (auto &p : ans)
        cout << p.first << " " << p.second << '\n';
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) solve();
    return 0;
}

Answer: 1


#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ar(n) int a[n]; for(int i=0;i<n;i++){cin>>a[i];}
#define br(n) int b[n]; for(int i=0;i<n;i++){cin>>b[i];}
//stalk mt kr bkl
typedef tree<int, null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
// ye tree vale me first vala datatype h less means ascending order rkhna h greater se descending order hojaega, less_equal se ascending+multiple values
// XOR(l,r) can be found as XOR(0,r)⊕XOR(0,l−1) this is in O(1).
// fibonnaci me agar koi number k se modulo ho toh a,b,c sequence h agar, then c=a+b or c=(a+b)%k dono ka %k same hoga throughout
bool comp(pair<int,int> p1, pair<int,int> p2) {
        if(p1.first > p2.first) return true;
        if(p1.first < p2.first) return false;
        if(p1.first == p2.first){
            if(p1.second<p2.second) return true;

            return false;
        }
    }

int mod_exp(int base, int exp, int mod) {
    int result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        exp /= 2;
    }
    return result;
}
int modinv(int a, int mod) {
    return mod_exp(a, mod - 2, mod);
}
int ap(int a,int n,int d){
  int sum=n*(2*a+(n-1)*d)/2;
  return sum;
}
bool isPrime(int n) {
    // 1 is not prime
    if (n <= 1) return false;

    // 2 and 3 are prime numbers
    if (n == 2 || n == 3) return true;

    // Check for divisibility by 2 or 3 to handle even and multiples of 3
    if (n % 2 == 0 || n % 3 == 0) return false;

    // Check divisibility from 5 to sqrt(n), skipping even numbers
    for (int i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }

    return true;
}
bool isSorted(vector<int>& b) {
    for (int i = 0; i < b.size() - 1; ++i) {
        if (b[i] > b[i + 1]) return false;
    }
    return true;
}
int findMex(const int arr[], int start, int end) {
    std::unordered_set<int> elements;  // To store unique elements for O(1) lookup

    // Insert all elements from the array into the set
    for (int i = start; i <= end; ++i) {
        elements.insert(arr[i]);
    }

    int mex = 0;
    // Increment mex until we find a value not in the set
    while (elements.find(mex) != elements.end()) {
        ++mex;
    }
    //int mex = findMex(arr, size);
    return mex;
}
vector<int> computeSuffixMex(const int arr[], int n) {
    unordered_set<int> seen;
    vector<int> suffixMex(n);
    int mex = 0;

    for (int i = n - 1; i >= 0; --i) {
        seen.insert(arr[i]);

        // Update mex to be the smallest non-negative integer not in the seen set
        while (seen.find(mex) != seen.end()) {
            ++mex;
        }

        suffixMex[i] = mex;
    }

    return suffixMex;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t=1;
    //cin>>t;
    int modu=(1e9)+7;
    while (t--) {
        int n;
        cin>>n;
        ar(n);
        vector<pair<int,int>> v;
        for(int i=n;i>0;i--){
            for(int j=i-1;j>0;j--){
                v.push_back({i,j});
            }
        }
        vector<int> fr(n+1,0);
        for(int i=0;i<n;i++){
            for(int j=a[i]+1;j<=n;j++){
                if(fr[j]==0){
                v.push_back({a[i],j});
                }
            }
            fr[a[i]]++;
        }
        cout<<v.size()<<endl;
        for(int i=0;i<v.size();i++){
            cout<<v[i].first<<' '<<v[i].second<<endl;
        }
    }
    return 0;
}

#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define ar(n) int a[n]; for(int i=0;i<n;i++){cin>>a[i];}
#define br(n) int b[n]; for(int i=0;i<n;i++){cin>>b[i];}
//stalk mt kr bkl
typedef tree<int, null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> pbds; // find_by_order, order_of_key
// ye tree vale me first vala datatype h less means ascending order rkhna h greater se descending order hojaega, less_equal se ascending+multiple values
// XOR(l,r) can be found as XOR(0,r)⊕XOR(0,l−1) this is in O(1).
// fibonnaci me agar koi number k se modulo ho toh a,b,c sequence h agar, then c=a+b or c=(a+b)%k dono ka %k same hoga throughout
bool comp(pair<int,int> p1, pair<int,int> p2) {
        if(p1.first > p2.first) return true;
        if(p1.first < p2.first) return false;
        if(p1.first == p2.first){
            if(p1.second<p2.second) return true;

            return false;
        }
    }

int mod_exp(int base, int exp, int mod) {
    int result = 1;
    while (exp > 0) {
        if (exp % 2 == 1) {
            result = (result * base) % mod;
        }
        base = (base * base) % mod;
        exp /= 2;
    }
    return result;
}
int modinv(int a, int mod) {
    return mod_exp(a, mod - 2, mod);
}
int ap(int a,int n,int d){
  int sum=n*(2*a+(n-1)*d)/2;
  return sum;
}
bool isPrime(int n) {
    // 1 is not prime
    if (n <= 1) return false;

    // 2 and 3 are prime numbers
    if (n == 2 || n == 3) return true;

    // Check for divisibility by 2 or 3 to handle even and multiples of 3
    if (n % 2 == 0 || n % 3 == 0) return false;

    // Check divisibility from 5 to sqrt(n), skipping even numbers
    for (int i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }

    return true;
}
bool isSorted(vector<int>& b) {
    for (int i = 0; i < b.size() - 1; ++i) {
        if (b[i] > b[i + 1]) return false;
    }
    return true;
}
int findMex(const int arr[], int start, int end) {
    std::unordered_set<int> elements;  // To store unique elements for O(1) lookup

    // Insert all elements from the array into the set
    for (int i = start; i <= end; ++i) {
        elements.insert(arr[i]);
    }

    int mex = 0;
    // Increment mex until we find a value not in the set
    while (elements.find(mex) != elements.end()) {
        ++mex;
    }
    //int mex = findMex(arr, size);
    return mex;
}
vector<int> computeSuffixMex(const int arr[], int n) {
    unordered_set<int> seen;
    vector<int> suffixMex(n);
    int mex = 0;

    for (int i = n - 1; i >= 0; --i) {
        seen.insert(arr[i]);

        // Update mex to be the smallest non-negative integer not in the seen set
        while (seen.find(mex) != seen.end()) {
            ++mex;
        }

        suffixMex[i] = mex;
    }

    return suffixMex;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t=1;
    //cin>>t;
    int modu=(1e9)+7;
    while (t--) {
        int n;
        cin>>n;
        ar(n);
        vector<pair<int,int>> v;
        for(int i=0;i<n;i++){
            vector<pair<int,char>>bona;
            for(int j=i+1;j<n;j++){
                if(a[j]>a[i]){
                    bona.push_back({a[j],'b'});
                }else{
                    bona.push_back({a[j],'c'});
                }
            }
            sort(bona.rbegin(),bona.rend());
            for(int k=0;k<bona.size();k++){
                if(bona[k].second=='b'){
                    v.push_back({bona[k].first,a[i]});
                    v.push_back({a[i],bona[k].first});
                }else{
                    v.push_back({a[i],bona[k].first});
                }
            }
        }
        for(int i=0;i<v.size();i++){
            cout<<v[i].first<<' '<<v[i].second<<endl;
        }
    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

/*
  #include<Luck>
  Compete against Yourself.
  Author - swayam_wish
*/
using namespace std;

#define ll long long
#define pii pair<ll, ll>
#define piii pair<ll, pair<ll, ll>>
#define pis pair<int, string>
#define db double

ll gcd(ll a, ll b) {
    if (a % b == 0) return b;
    return gcd(b, a % b);
}

ll reqN(ll k){
   int cnt = 0;

   bool ok = 0;

   while(k != 1){
      if(k%2 != 0){
        ok = 1;
      }
      k/=2;
      cnt++;

   }
   return cnt+ok;
}

int main() {

    int t ;
    t= 1;

    while (t--) {

    int n;
    cin >> n;

    vector<ll> nums(n);

    for(int i = 0; i<n; i++){
        cin >> nums[i];
    }

    vector<vector<ll>> ans;
    vector<vector<ll>> seq(n+1,vector<ll>(n+1,0));
    for(int i = 0; i<n; i++){
        for(int j = i+1; j<n; j++){
            seq[nums[j]][nums[i]] = 1;
            if(nums[i]<nums[j]){
                ans.push_back({nums[j],nums[i]});
                ans.push_back({nums[i],nums[j]});
            }
            else{
                ans.push_back({nums[i],nums[j]});
            }
        }
    }

    sort(ans.begin(),ans.end(),[seq](vector<ll> &a ,vector<ll> & b){
        int curr1 = seq[a[0]][a[1]];
        int curr2 = seq[b[0]][b[1]];

        if(curr1 != curr2){
            return curr1>curr2;
        }
        else{
            if(a[0] == b[0]){
                return a[1]<b[1];
            }
            return a[0]>b[0];
        }
    });
    cout<<ans.size()<<endl;
    for(auto it: ans){
        cout<<it[0]<<" "<<it[1]<<endl;
    }

    }

    return 0;
}

#include <bits/stdc++.h>

/*
  #include<Luck>
  Compete against Yourself.
  Author - swayam_wish
*/
using namespace std;

#define ll long long
#define pii pair<ll, ll>
#define piii pair<ll, pair<ll, ll>>
#define pis pair<int, string>
#define db double

ll gcd(ll a, ll b) {
    if (a % b == 0) return b;
    return gcd(b, a % b);
}

ll reqN(ll k){
   int cnt = 0;

   bool ok = 0;

   while(k != 1){
      if(k%2 != 0){
        ok = 1;
      }
      k/=2;
      cnt++;

   }
   return cnt+ok;
}

int main() {

    int t ;
    t= 1;

    while (t--) {

    int n;
    cin >> n;

    vector<ll> nums(n);

    for(int i = 0; i<n; i++){
        cin >> nums[i];
    }

    vector<vector<ll>> ans;
    vector<vector<ll>> seq(n+1,vector<ll>(n+1,0));
    int cnt = 0;
    for(int i = 0; i<n; i++){
        for(int j = i+1; j<n; j++){

            if(nums[i]<nums[j]){
                seq[nums[j]][nums[i]]=1;
                seq[nums[i]][nums[j]]=1;
                cnt+=2;
            }
            else{

                seq[nums[i]][nums[j]]=1;
                cnt++;
            }
        }
    }

    vector<ll> cnums(n);
    for(int i = 0; i<n; i++){
        cnums[i] = i+1;
    }

    while(cnt){
        for(int i = 0; i<n-1; i++){
            if(seq[cnums[i+1]][cnums[i]] == 1){
                cnt--;
                ans.push_back({cnums[i+1],cnums[i]});
                seq[cnums[i+1]][cnums[i]] = 0;
                swap(cnums[i],cnums[i+1]);

            }
        }

    }
    cout<<ans.size()<<endl;
    for(auto it : ans){
        cout<<it[0]<<" "<<it[1]<<endl;
    }

    }

    return 0;
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define all(X) (X).begin(), (X).end()
#define sz(X) ((int)(X).size())

#define st first
#define nd second

using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;

void solvesub(int n, const vector<int>& pos, vector<int>& curr, vector<pll>& ans) {
    int i = n - 1;
    while(i > 0 && pos[curr[i]] < pos[curr[i - 1]]) {
        ans.push_back({curr[i], curr[i - 1]});
        swap(curr[i], curr[i - 1]);
        i--;
    }
    for(int j = i - 1; j >= 0; j--) {
        ans.push_back({curr[i], curr[j]});
    }
    for(int j = 0; j < i; j++) {
        ans.push_back({curr[j], curr[i]});
    }

    cout << "\n\n";
    for(auto v : curr) {
        cout << v + 1 << " ";
    }
    cout << "\n\n";
}

void solve() {
    int n;
    cin >> n;
    vector<int> pos(n);
    for(int i = 0; i < n; i++) {
        int v;
        cin >> v;
        v--;
        pos[v] = i;
    }
    vector<int> curr(n);
    iota(curr.begin(), curr.end(), 0);
    vector<pll> ans;
    for(int i = 1; i < n; i++) {
        solvesub(i + 1, pos, curr, ans);
    }

    cout << ans.size() << "\n";
    for(auto v : ans) {
        cout << v.first + 1 << " " << v.second + 1 << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define all(X) (X).begin(), (X).end()
#define sz(X) ((int)(X).size())

#define st first
#define nd second

using ll = long long;
using ld = long double;
using pll = pair<ll,ll>;

void solvesub(int n, const vector<int>& pos, vector<int>& curr, vector<pll>& ans) {
    int i = n - 1;
    while(i > 0 && pos[curr[i]] < pos[curr[i - 1]]) {
        ans.push_back({curr[i], curr[i - 1]});
        swap(curr[i], curr[i - 1]);
        i--;
    }
    for(int j = i - 1; j >= 0; j--) {
        ans.push_back({curr[i], curr[j]});
    }
    for(int j = 0; j < i; j++) {
        ans.push_back({curr[j], curr[i]});
    }
}

void solve() {
    int n;
    cin >> n;
    vector<int> pos(n);
    for(int i = 0; i < n; i++) {
        int v;
        cin >> v;
        v--;
        pos[v] = i;
    }
    vector<int> curr(n);
    iota(curr.begin(), curr.end(), 0);
    vector<pll> ans;
    for(int i = 1; i < n; i++) {
        solvesub(i + 1, pos, curr, ans);
    }

    cout << ans.size() << "\n";
    for(auto v : ans) {
        cout << v.first + 1 << " " << v.second + 1 << "\n";
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
    return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod=1e9+7;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
 ll n;
 cin>>n;
 vector<ll>final(n+1);unordered_map<ll,ll>mp;
 for(ll i=1;i<=n;i++) cin>>final[i];
 for(ll i=1;i<=n;i++)
 {
    mp[final[i]]=i;
 }
 vector<pair<ll,ll>>ans;
 for(ll i=1;i<=n-1;i++)
 {
    for(ll j=i+1;j<=n;j++) ans.push_back({j,i});
 }
 vector<ll>crr(n+1);
 for(ll i=n;i>=1;i--) crr[i]=n-i+1;
 while(true)
 { bool flag=false;
    for(ll i=1;i<=n-1;i++)
    {
        ll a=crr[i]; ll b=crr[i+1];
if((b<a && mp[a]>mp[b]) ) {flag=true; swap(crr[i],crr[i+1]); ans.push_back({b,a});}
    }
    if(!flag) break;
 }
 cout<<ans.size()<<endl;

 for(auto &it:ans) cout<<it.first<<" "<<it.second<<endl;

}

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll mod=1e9+7;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
 ll n;
 cin>>n;
 vector<ll>final(n+1);unordered_map<ll,ll>mp;
 for(ll i=1;i<=n;i++) cin>>final[i];
 for(ll i=1;i<=n;i++)
 {
    mp[final[i]]=i;
 }
 vector<pair<ll,ll>>ans;
 for(ll i=1;i<=n-1;i++)
 {
    for(ll j=i+1;j<=n;j++)
    {
        if(mp[j]<mp[i]) {ans.push_back({j,i}); }
        else {
            ans.push_back({j,i});
            ans.push_back({i,j});
        }
    }
 }
 cout<<ans.size()<<endl;

 for(auto &it:ans) cout<<it.first<<" "<<it.second<<endl;

}

Answer: 1


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

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

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

//------------------------------------------------//
#if __has_include("../files/juzar_7.cpp")
#include "../files/juzar_7.cpp"
#endif
#ifdef juzZ
#define deb(x) {cerr << #x << " "; _print(x); cerr << "\n";}
#else
#define deb(x)
#endif
#define rep(i, a, b) for (int i = a; i < b; i++)
#define precision(a) cout << setprecision(a) << fixed
#define MSB(x) (63 - __builtin_clzll(x))
#define amax(a, b) a = max(a, b)
#define amin(a, b) a = min(a, b)
#define MP make_pair
#define all(a) a.begin(), a.end()
template <class T> ostream &operator<<(ostream &os, const vector<T> a){ for (auto ele : a) os << ele << " "; return os;}
template <class T> istream &operator>>(istream &is, vector<T> &a){ for (auto &ele : a) is >> ele; return is; }
typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<long long> vl; typedef vector<vector<long long>> vvl; typedef vector<bool> vb; typedef vector<vector<bool>> vvb; typedef map<int, int> mii; typedef map<long long, long long> mll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef unsigned long long ull; typedef long double ld; typedef long long ll;
//------------------------------------------------//

ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll binExp(ll a, ll b) {ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; }
ll MOD = 1e9 + 7;

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

void solve(int loop)
{
    int n;
    cin>>n;
    vector<int> v(n+1);
    for (int i=1; i<n+1; i++) cin>>v[i];
    // int ans = 0;
    set<pair<int, int>> st;
    vector<int> ini(n+1);
    for (int i=1; i<=n; i++) ini[i] = i;
    vector<pair<int, int>> ans;
    for (int i=1; i<=n; i++)
    {
        int curr = v[i];
        int idx = -1;
        for (int j=1; j<=n; j++)
        {
            if (ini[j]==curr)
            {
                idx = j;
                break;
            }
        }
        if (idx==i) continue;
        for (int j=idx; j>i; j--)
        {
            ans.push_back({ini[j], ini[j-1]});
            swap(ini[j], ini[j-1]);
            st.insert({min(ini[j], ini[j-1]), max(ini[j], ini[j-1])});
        }
    }
    for (int i=1; i<n; i++)
    {

        int x = min(ini[i], ini[i+1]);
        int y = max(ini[i], ini[i+1]);
        if (st.find({x, y})==st.end())
        {
            ans.push_back({ini[i+1], ini[i]});
            ans.push_back({ini[i], ini[i+1]});
            st.insert({x, y});
        }

    }
    cout << ans.size() << "\n";
    for (auto it : ans) cout << it.first << " " << it.second << "\n";
}

int32_t main()
{
// #define DEBUG_MODE
#ifndef DEBUG_MODE
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

#ifdef juzZ
    freopen("../files/input.txt", "r", stdin); freopen("../files/output.txt", "w", stdout); freopen("../files/error.txt", "w", stderr);
#endif
#else
#ifdef juzZ
    freopen("../files/input.txt", "r", stdin);
#endif
#endif

    int tc = 1;
    // cin >> tc;
    for (int loop = 1; loop <= tc; loop++)
    {
        // cout << "Case #" << loop << ": ";
        solve(loop);
        // bool as = solve(); cout << (as ? "YES\n" : "NO\n");
        // int as = solve(); cout << as << "\n";
    }
    return 0;
}

/*
    Wed Mar  1 19:52:06 2023
    File : C.cpp
*/

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

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

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

//------------------------------------------------//
#if __has_include("../files/juzar_7.cpp")
#include "../files/juzar_7.cpp"
#endif
#ifdef juzZ
#define deb(x) {cerr << #x << " "; _print(x); cerr << "\n";}
#else
#define deb(x)
#endif
#define rep(i, a, b) for (int i = a; i < b; i++)
#define precision(a) cout << setprecision(a) << fixed
#define MSB(x) (63 - __builtin_clzll(x))
#define amax(a, b) a = max(a, b)
#define amin(a, b) a = min(a, b)
#define MP make_pair
#define all(a) a.begin(), a.end()
template <class T> ostream &operator<<(ostream &os, const vector<T> a){ for (auto ele : a) os << ele << " "; return os;}
template <class T> istream &operator>>(istream &is, vector<T> &a){ for (auto &ele : a) is >> ele; return is; }
typedef vector<int> vi; typedef vector<vector<int>> vvi; typedef vector<long long> vl; typedef vector<vector<long long>> vvl; typedef vector<bool> vb; typedef vector<vector<bool>> vvb; typedef map<int, int> mii; typedef map<long long, long long> mll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; typedef unsigned long long ull; typedef long double ld; typedef long long ll;
//------------------------------------------------//

ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll binExp(ll a, ll b) {ll res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; }
ll MOD = 1e9 + 7;

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

void solve(int loop)
{
    int n;
    cin>>n;
    vector<int> v(n+1, 0);
    for (int i=1; i<=n; i++) cin>>v[i];
    reverse(v.begin()+1, v.end());
    vector<int> ini(n+1, 0);
    for (int i=1; i<=n; i++) ini[i] = i;
    vector<pair<int, int>> ans;
    for (int i=1; i<=n; i++)
    {
        int curr = v[i];
        int idx = -1;
        for (int j=i; j<=n; j++)
        {
            if (ini[j]==curr)
            {
                idx = j;
                break;
            }
        }
        for (int j=idx; j>i; j--)
        {
            ans.push_back({ini[j], ini[j-1]});
            swap(ini[j], ini[j-1]);
        }
    }
    reverse(v.begin()+1, v.end());
    for (int i=1; i<=n; i++)
    {
        int curr = v[i];
        int idx = -1;
        for (int j=i; j<=n; j++)
        {
            if (ini[j]==curr)
            {
                idx = j;
                break;
            }
        }
        for (int j=idx; j>i; j--)
        {
            ans.push_back({ini[j], ini[j-1]});
            swap(ini[j], ini[j-1]);
        }
    }
    cout << ans.size() << "\n";
    for (auto it : ans) cout << it .first << " " << it.second << "\n";
}

int32_t main()
{
// #define DEBUG_MODE
#ifndef DEBUG_MODE
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

#ifdef juzZ
    freopen("../files/input.txt", "r", stdin); freopen("../files/output.txt", "w", stdout); freopen("../files/error.txt", "w", stderr);
#endif
#else
#ifdef juzZ
    freopen("../files/input.txt", "r", stdin);
#endif
#endif

    int tc = 1;
    // cin >> tc;
    for (int loop = 1; loop <= tc; loop++)
    {
        // cout << "Case #" << loop << ": ";
        solve(loop);
        // bool as = solve(); cout << (as ? "YES\n" : "NO\n");
        // int as = solve(); cout << as << "\n";
    }
    return 0;
}

/*
    Wed Mar  1 19:52:06 2023
    File : C.cpp
*/

Answer: 2


/*'ॐ नमः शिवाय'*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int mod = 1e9 + 7;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), c(n);
    map<int, int>ind, m;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        ind[c[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        a[i] = i + 1;
        m[a[i]] = i;
    }
    vector<pair<int, int>> v;
    for (int i = 0; i < n; i++)a[i] = i + 1;
    int i = n - 1;
    int ans = 0;
    while (i >= 0) {
        int j = m[c[i]];
        while (j < i) {
            v.push_back({a[j + 1], a[j]});
            swap(a[j], a[j + 1]);
            ans++;
            j++;
        }
        for (int i = 0; i < n; i++) {
            m[a[i]] = i;
        }
        i--;
    }
    vector<pair<int, int>> temp;
    for (int i = n; i >= 1; i--) {
        int j = ind[i];
        while (j >= 0) {
            if (c[j - 1] < i) {
                v.push_back({i, c[j - 1]});
                temp.push_back({i, c[j - 1]});
                ans += 2;
            }
            j--;
        }
    }
    int size = temp.size();
    for (int j = size - 1; j >= 0; j--) {
        int x = temp[j].first;
        int y = temp[j].second;
        v.push_back({y, x});
    }
    cout << ans << endl;
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].first << " " << v[i].second << endl;
    }
}

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

#ifndef ONLINE_JUDGE
    freopen("input.txt",  "r",  stdin);
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    //cin >> t;
    while (t--) solve();

    return 0;
}

/*'ॐ नमः शिवाय'*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int mod = 1e9 + 7;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n), c(n);
    map<int, int>ind, m;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        ind[c[i]] = i;
    }
    for (int i = 0; i < n; i++) {
        a[i] = i + 1;
        m[a[i]] = i;
    }
    map<pair<int, int>, int> mp;
    vector<pair<int, int>> v;
    for (int i = 0; i < n; i++)a[i] = i + 1;
    int i = n - 1;
    int ans = 0;
    while (i >= 0) {
        int j = m[c[i]];
        while (j < i) {
            v.push_back({a[j + 1], a[j]});
            swap(a[j], a[j + 1]);
            ans++;
            j++;
        }
        for (int i = 0; i < n; i++) {
            m[a[i]] = i;
        }
        i--;
    }
    vector<pair<int, int>> temp;
    for (int i = n; i >= 1; i--) {
        int j = ind[i];
        while (j >= 0) {
            if (c[j - 1] < i) {
                v.push_back({i, c[j - 1]});
                temp.push_back({i, c[j - 1]});
                ans += 2;
            }
            j--;
        }
    }
    int size = temp.size();
    for (int j = size - 1; j >= 0; j--) {
        int x = temp[j].first;
        int y = temp[j].second;
        v.push_back({y, x});
    }
    cout << ans << endl;
    for (int i = 0; i < v.size(); i++) {
        cout << v[i].first << " " << v[i].second << endl;
    }
}

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

#ifndef ONLINE_JUDGE
    freopen("input.txt",  "r",  stdin);
    freopen("output.txt", "w", stdout);
#endif

    int t = 1;
    cin >> t;
    while (t--) solve();

    return 0;
}

Answer: 1