Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

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

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

int q;
string X,Y;
int L[1001][1001];

int max(int i,int j) {return i > j ? i : j;}

int getL(int i,int j) {
	if(L[i][j] != -1) return L[i][j];

	if(i == 0 || j == 0) return 0;
	if(X[i - 1] == Y[j - 1]) return 1 + getL(i - 1,j - 1);
	return max(getL(i,j - 1),getL(i - 1,j));
}

int main() {

	cin >> q;
	for(int i = 0;i < q;i++) {
		cin >> X >> Y;
		for(int j = 0;j <= X.size();j++) {
			for(int k = 0;k <= Y.size();k++) L[j][k] = -1;
		}
		cout << getL(X.size(),Y.size()) << endl;
	}
}

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

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int N;
  cin>>N;
  string S1,S2;
  for (int k=0; k<N; k++) {
    cin>>S1>>S2;
    int m=S1.length();
    int n=S2.length();
    S1=' '+S1;
    S2=' '+S2;
    int lcs[1001][1001];
    for (int i=1; i<=m; i++) {
      lcs[i][0]=0;
    }
    for (int j=1; j<=n; j++) {
      lcs[0][j]=0;
    }
    int maxl=0;
    for (int i=1; i<=m; i++) {
      for (int j=1; j<=n; j++) {
        if (S1[i]==S2[j]) {
          lcs[i][j]=lcs[i-1][j-1]+1;
        }
        else {
          lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
        }
        maxl=max(maxl,lcs[i][j]);
      }
    }
    cout<<maxl<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N =1000;

int lcs(string Xtw,string Ytw){
    int c[N+1][N+1];
    int m=Xtw.size();
    int n=Ytw.size();
    int maxl=0;
    Xtw=' '+Xtw;
    Ytw=' '+Ytw;
    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(Xtw[i]==Ytw[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 s1tw,s2tw;
    int n; cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1tw>>s2tw;
        cout<<lcs(s1tw,s2tw)<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include<algorithm>
using namespace std;
int main(void){
    int n;
    cin>>n;
    string x,y;
    for(int i=0;i<n;i++){
        cin>>x>>y;
        int c[x.size()+1][y.size()+1];
        for(int j=0;j<x.size()+1;j++){
            for(int k=0;k<y.size()+1;k++){
                if(j==0||k==0)c[j][k]=0;
                else if(j>0&&k>0&&x[j-1]==y[k-1]) c[j][k]=c[j-1][k-1]+1;
                else if(j>0&&k>0&&x[j-1]!=y[k-1])c[j][k]=max(c[j][k-1],c[j-1][k]);
            }
        }
        int maxi=0;
        for(int j=0;j<x.size()+1;j++){
            for(int k=0;k<y.size()+1;k++){
               maxi=max(maxi,c[j][k]);
               c[j][k]=0;
            }
        }
        cout<<maxi<<endl;
    }
    
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define ll long long
#define endl '\n'
using namespace std;
int dy[4] = { 1, 0, -1, 0 }, dx[4] = { 0, 1, 0, -1 };
typedef pair<ll, ll> P;
const int MAX_N = 1000;
int dp[MAX_N + 1][MAX_N + 1];
string x, y;
int q;

int solve(string x, string y)
{
    int n = x.size(), m = y.size();
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (x[i] == y[j]) {
                dp[i + 1][j + 1] = dp[i][j] + 1;
            } else {
                dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
            }
        }
    }
    return dp[n][m];
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        cout << solve(x, y) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
static const int Max = 1000;
int getlong(string a, string b){
int n,m,i,j,as=0;
	int cs[Max + 1][Max + 1];
	n=a.size();
	m=b.size();

	a=' '+a;
	b=' '+b;
	cs[0][0]=0;
	for(i=1;i<=n;i++)
	cs[i][0]=0;
	for(i=1;i<=m;i++)
	cs[0][i]=0;
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++){
			if(a[i]==b[j])
			cs[i][j]=cs[i-1][j-1]+1;
			else
			cs[i][j]=max(cs[i][j-1],cs[i-1][j]);
		as=max(as,cs[i][j]);
		}
		
	}
	return as;
}
main(){
	int q,i;
	string a,b;
	cin>>q;
	for(i=0;i<q;i++){
		cin>>a>>b;
		printf("%d\n",getlong(a,b));
	}
	return 0;
}

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

using namespace std;

int main() {
	int n, l1, l2;
	string s1, s2;

	cin >> n;
	while (n--) {
		cin >> s1 >> s2;
		l1 = s1.size();
		l2 = s2.size();

		int dp[l1 + 1][l2 + 1];
		for (int i = 0; i <= l1; i++) {
			for (int j = 0; j <= l2; j++) {
				dp[i][j] = 0;
			}
		}
		for (int i = 1; i <= l1; i++) {
			for (int j = 1; j <= l2; j++) {
				if (s1[i - 1] == s2[j - 1]) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				}
				else {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				}
			}
		}
		cout << dp[l1][l2] << endl;
	}

	return 0;
}