Go: package main

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

func main() {
	for q := ReadInt(); q > 0; q-- {
		x, y := ReadString(), ReadString()
		fmt.Println(LCS(x, y))
	}
}

func LCS(x, y string) int {
	dp := make([][]int, len(x)+1)
	for i := 0; i < len(x)+1; i++ {
		dp[i] = make([]int, len(y)+1)
	}
	for i := 0; i < len(x); i++ {
		for j := 0; j < len(y); j++ {
			dp[i+1][j+1] = Max(dp[i+1][j], dp[i][j+1])
			if x[i] == y[j] {
				dp[i+1][j+1] = Max(dp[i+1][j+1], dp[i][j]+1)
			}
		}
	}
	return dp[len(x)][len(y)]
}

func Max(xs ...int) int {
	max := xs[0]
	for _, x := range xs[1:] {
		if max < x {
			max = x
		}
	}
	return max
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int dat[1024][1024];
int getLcbLen(char* x, int xe, char* y, int ye){
	if(xe <= 0 || ye <= 0) return 0;
	if(dat[xe][ye] != -1) return dat[xe][ye];
	return dat[xe][ye] = (x[xe-1] == y[ye-1] ? 1 + getLcbLen(x, xe-1, y, ye-1) : max(getLcbLen(x, xe-1, y, ye), getLcbLen(x, xe, y, ye-1)));
}

int main() {
	int set;
	scanf("%d", &set);
	
	while(set--){
		char x[1024], y[1024];
		scanf("%s %s", x, y);
		int xe = (int)strlen(x), ye = (int)strlen(y);
		
		for(int i = 0; i < xe; i++){
			for(int k = 0; k < ye; k++){
				dat[i][k] = -1;
			}
		}
		printf("%d\n", getLcbLen(x, xe, y, ye));
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <cmath>
#include <sstream>
#define F first
#define S second
#define FOR(i,n) for(int i=0;i<(int)n;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define FORI(i,k,n) for(int i=k;i<(int)n;i++)
#define repp(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
typedef pair<double, double> pdd;
typedef pair<int,int> pii;
const double EPS = 10e-6;
int c[2000][2000]={};
string x,y;
int dp(int o,int p)
{
	if(o==x.size()||p==y.size())return 0;
	if(c[o][p]!=-1)return c[o][p];
	int ans =0;
	if(x[o]==y[p])
	{
		ans=max(dp(o+1,p+1)+1,max(dp(o+1,p),dp(o,p+1)));
	}
	else ans=max(dp(o+1,p),dp(o,p+1));

	return c[o][p]=ans;
}

int main ()
{
	int n;
	cin>>n;
	rep(i,n)
	{
		
		cin>>x>>y;
		memset(c, -1, sizeof(c));
		cout<<dp(0,0)<<endl;;
	}



}

C++: #include "bits/stdc++.h"
using namespace std;

int main() {
	long long Q; cin >> Q;
	for (long long i = 0; i < Q; i++) {
		string X, Y; cin >> X >> Y;
		long long X_size = X.size(), Y_size = Y.size();
		vector<vector<long long>>DP(1100, vector<long long>(1100, 0));
		for (long long j = 0; j < X_size; j++) {
			DP.at(j).at(0) = 0;
		}
		for (long long j = 0; j < Y_size; j++) {
			DP.at(0).at(j) = 0;
		}
		for (long long j = 0; j < X_size; j++) {
			for (long long k = 0; k < Y_size; k++) {
				if (X.at(j) == Y.at(k)) DP.at(j + 1).at(k + 1) = max({ DP.at(j).at(k) + 1, DP.at(j).at(k + 1), DP.at(j + 1).at(k) });
				else DP.at(j + 1).at(k + 1) = max(DP.at(j).at(k + 1), DP.at(j + 1).at(k));
			}
		}
		cout << DP.at(X_size).at(Y_size) << endl;
	}
}

C++: #include <iostream>
#include <string>

using namespace std;

int main(){

  int q;

  cin >> q;


  for(int k = 0; k < q; k++){
    string a, b;
    cin >> a >> b;
    int dp[a.size()][b.size()] = {};
    //初期設定
    if(a[0] == b[0]){
      dp[0][0] = 1;
    }
    for(int i = 1; i < a.size(); i++){
      if(a[i] == b[0] || dp[i-1][0] == 1){
        dp[i][0] = 1;
      }
    }
    for(int j = 1; j < b.size(); j++){
      if(a[0] == b[j] || dp[0][j-1] == 1){
        dp[0][j] = 1;
      }
    }
    for(int i = 1; i < a.size(); i++){
      for(int j = 1; j < b.size(); j++){
        if(a[i] == b[j]){
          if(dp[i-1][j-1] >= dp[i-1][j] && dp[i-1][j-1] >= dp[i][j-1]){
            dp[i][j] = dp[i-1][j-1]+1;

          }
          else{
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
          }
        }
        else{
          dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    cout << dp[a.size()-1][b.size()-1] << "\n";
  }

  return 0;
}

C++: #include <iostream>

using namespace std;

int LCS[1001][1001];

int lcs(string X, string Y)
{
    int m = X.size();
    int n = Y.size();
    X = ' ' + X;
    Y = ' ' + Y;
    for(int i = 0; i <= m; i++)
        LCS[i][0] = 0;
    for(int j = 1; j <= n; j++)
        LCS[0][j] = 0;
    
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(X[i] == Y[j])
                LCS[i][j] = LCS[i - 1][j - 1] + 1;
            else
                LCS[i][j] = max(LCS[i][j - 1], LCS[i - 1][j]);
        }
    }
    return LCS[m][n];
}

int main()
{
    int q;
    cin >> q;
    string X;
    string Y;
    for(int i = 0; i < q; i++)
    {
        cin >> X;
        cin >> Y;
        cout << lcs(X, Y) << endl;
    }
}

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 = 1; 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 q;
  cin >> q;

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

  return 0;
}

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

int main(){
	int q;
	scanf(" %d", &q);
	for(int i=0; i<q; ++i){
		string a, b;
		cin >> a >> b;
		
		int m=a.size(), n=b.size();
		
		string x=" "+a;
		string y=" "+b;
		
		int c[1001][1001]={0};
		for(int i=1; i<=m; ++i) c[i][0]=0;
		for(int i=1; i<=n; ++i) c[0][n]=0;
		
		int ans=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][j-1], c[i-1][j]);
				
				ans=max(ans,c[i][j]);		
			}
		}
		
		printf("%d\n", ans);
	}
}

C++: #include <iostream>
#include <string>

#define MAX_N 1000
#define MAX_M 1000

int n, m;
std::string s, t;

int dp[MAX_N+1][MAX_M+1];

void solve() {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i+1] == t[j+1]) {
        dp[i+1][j+1] = dp[i][j] + 1;
      }
      else {
        dp[i+1][j+1] = std::max(dp[i][j+1], dp[i+1][j]);
      }
    }
  }
}

int main() {
  int q;
  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    std::cin >> s >> t;
    n = s.size();
    m = t.size();
    solve();
    std::cout << dp[n][m] << std::endl;
  }
}

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;
  }
return 0;
}