Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

C++: #include <iostream>
#include <string>
#include <vector>
using namespace std;
void input(string &x,string &y){
	cin>>x>>y;
}
int lcs(string x,string y){
	vector<vector<int>> tb(vector<vector<int>>((int)x.size()+1,vector<int>(y.size()+1,0)));
	for(int i=1;i<(int)x.size()+1;i++){
		for(int j=1;j<(int)y.size()+1;j++){
			if(x[i-1]==y[j-1])  tb[i][j]=tb[i-1][j-1]+1;
			else tb[i][j]=max(tb[i-1][j],tb[i][j-1]);
		}
	}
	return tb[(int)x.size()][(int)y.size()];
}
int main(){
	int q;
	cin>>q;
	string x,y;
	for(int i=0;i<q;i++){
		input(x,y);
		cout<<lcs(x,y)<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;

static const int N = 1000;

int Lcs(string X, string Y) {
    int c[N + 1][N + 1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for (int i = 0; i <= m; i++) {c[i][0] = 0;}
    for (int j = 1; j <= n; j++) {c[0][j] = 0;}

    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if( X[i] == Y[j] ) {
                c[i][j] = c[i - 1][j - 1] + 1;
            }
            else {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }

    return maxl;
}

int main() {
    string s1, s2;
    int n; 
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        cout << Lcs(s1, s2) << endl;
    }
    // for (int i = 0; i < n; i++) {
    //     cout << Lcs(s1[i], s2[i]) << endl;
    // }

    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int cnt;
  cin>>cnt;
  while(cnt--){
    string a,b;
    cin>>a>>b;
    int dp[1000][1000]={};
    for(int i=0;i<b.size();i++){
      int nm=0;
      for(int j=0;j<a.size();j++){
	if(a[j]==b[i])dp[i][j]=nm+1;
	if(i){
	  dp[i][j]=max(dp[i][j],dp[i-1][j]);
	  nm=max(nm,dp[i-1][j]);
	}
      }
    }
    int ans=0;
    for(int i=0;i<a.size();i++)ans=max(ans,dp[b.size()-1][i]);
    cout<<ans<<endl;
   
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for(int i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll q;
  cin >> q;
  rep(_, q) {
    string x, y;
    cin >> x >> y;
    ll m = x.size(), n = y.size();
    vector<vl> dp(m + 1, vl(n + 1, 0));
    rep(i, m) rep(j, n) {
      if(x[i] == y[j])
        dp[i + 1][j + 1] = dp[i][j] + 1;
      else
        dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
    }
    cout << dp[m][n] << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v
#define NUM(x,v) (POSU(x,v)-POSL(x,v))
const ll INF=1e9+7;
const ll N = 1002;
const ll M = 21;
ll q,dp[N][N];
string x,y;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>q;
	FOR(k,0,q){
		cin>>x>>y;
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				dp[i][j]=0;
			}
		}
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				if(i==0||j==0)dp[i][j]=0;
				else{
					if(x[i]==y[j])dp[i][j] = max(dp[i-1][j]+1,dp[i][j-1]+1);
					else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
				} 
			}
		}
		cout<<dp[x.size()][y.size()]<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;

#define MAX 1000

int lcs(string X, string Y){
    int c[MAX + 1][MAX + 1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for(int i = 0; i <= m; i++) c[i][0] = 0;
    for(int i = 1; i <= n; i++) c[0][i] = 0;
    cout << "test" << endl;
    for(int i = 1; i <= m; i++){
        for(int j = 1; j <= n; j++){
            if(X[i] == Y[j]){
                c[i][j] = c[i - 1][j - 1] + 1;
            }else{
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            maxl = max(maxl, c[i][j]);
        }        
    }
    return maxl;
}

int main(){
    string s1, s2;
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int lcs(string x, string y) {
  int dp[1001][1001];
  int maxl=0;
  x = ' ' + x;
  y = ' ' + y;
  for (int i = 0; i < x.size(); i++)
  {
    dp[i][0] = 0;
  }
  for (int i = 0; i < y.size(); i++)
  {
    dp[i][0] = 0;
  }
  for (int i = 1; i < x.size(); i++)
  {
    for (int j = 1; j < y.size(); j++)
    {
      if (x[i] == y[j]) {
        dp[i][j] = dp[i-1][j-1] + 1;
      }
      else {
        dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
      }
    maxl = max(maxl, dp[i][j]);
    }
    
  }
  return maxl;
}

int main(){

  //std::ifstream in ("input.txt");
  //std::cin.rdbuf(in.rdbuf());
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    string a, b;
    cin >> a >> b;
    cout << lcs(a, b) << endl;
  }
  return 0;
  
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<string>
using namespace std;

int LCS(string a, string b){
	int n=a.size();
	int m=b.size();
	int T[n+1][m+1];
	int i,j;
	for (i=0;i<n+1;i++) T[i][0]=i;
	for (j=0;j<m+1;j++) T[0][j]=j;
	for (i=1;i<n+1;i++){
		for (j=1;j<m+1;j++){
			int min=T[i-1][j]+1;
			if (min>T[i][j-1]+1){
				min=T[i][j-1]+1;
			}
			if ((a[i-1]==b[j-1])&&(min>T[i-1][j-1]+1)){
				min=T[i-1][j-1]+1;
			}
			T[i][j]=min;
		}
	}
	return n+m-T[n][m];
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n,i;
	cin >> n;
	
	string a,b;
	for (i=0;i<n;i++){
		cin >> a >> b;
		cout << LCS(a,b) << endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;

void solve(){
    string S, T;
    cin >> S;
    cin >> T;

    int dp[1001][1001];
    dp[0][0] = 0;

    for (int i = 0; i < S.size(); i++){
        for (int j = 0; j < T.size(); j++){
            if (S[i] == T[j])
                dp[i+1][j+1] = dp[i][j] + 1;
            else
                dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
        }
    }
    cout << dp[S.size()][T.size()] << endl;
}

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

    for (int i = 0; i < q; i++)
        solve();
}