Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

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

func lcs(a, b string) int {
	lenA := len(a)
	lenB := len(b)
	dp := make([][]int, lenA+1)
	for i := range dp {
		dp[i] = make([]int, lenB+1)
	}

	for i := 1; i <= lenA; i++ {
		for j := 1; j <= lenB; j++ {
			if a[i-1] == b[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i][j-1], dp[i-1][j])
			}
		}
	}
	return dp[lenA][lenB]
}

func main() {
	n := scanInt()
	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		a := scanString()
		b := scanString()
		fmt.Fprintln(w, lcs(a, b))
	}
	w.Flush()
}

C++: #include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll q;
  cin >> q;
  for (ll testcase = 0; testcase < q; ++testcase) {
    string a, b;
    cin >> a >> b;
    vector<vector<ll>> dp(a.size() + 1, vector<ll>(b.size() + 1, 0));
    for (ll i = 0; i < a.size(); ++i) {
      for (ll j = 0; j < b.size(); ++j) {
        if (a.at(i) == b.at(j)) {
          dp.at(i + 1).at(j + 1) = max(dp.at(i + 1).at(j + 1), dp.at(i).at(j) + 1);
        }
        dp.at(i + 1).at(j) = max(dp.at(i + 1).at(j), dp.at(i).at(j));
        dp.at(i).at(j + 1) = max(dp.at(i).at(j + 1), dp.at(i).at(j));
        dp.at(a.size()).at(j + 1) = max(dp.at(a.size()).at(j), dp.at(a.size()).at(j + 1));
      }
      dp.at(i + 1).at(b.size()) = max(dp.at(i + 1).at(b.size()), dp.at(i).at(b.size()));
    }
    cout << dp.at(a.size()).at(b.size()) << "\n";
    // for (ll i = 0; i <= a.size(); ++i) {
    //   for (ll j = 0; j <= b.size(); ++j) {
    //     cout << dp.at(i).at(j) << " ";
    //   }
    //   cout << "\n";
    // }
  }
}

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

int lcs(string X,string Y)
{
    int x=X.size(),y=Y.size();
    int dp[y+1][x+1];

    for(int i=0;i<x+1;i++)dp[0][i]=0;
    for(int i=1;i<y+1;i++)dp[i][0]=0;
    int maxl=0;
    
    for (int j = 1; j <= y; j++){
        for (int k = 1; k <= x ; k++){
            if (Y[j-1]==X[k-1]) dp[j][k]=dp[j-1][k-1]+1;
            else dp[j][k]=max(dp[j][k-1],dp[j-1][k]);
            maxl=max(maxl,dp[j][k]);
        }
    }
    return maxl;
}

int main()
{
    int q;
    cin>>q;
    string X,Y;
    for (int i = 0; i < q; i++) {
        cin>>X>>Y;
        cout<<lcs(X,Y)<<endl;
    }
}
/*
3
abcbdab
bdcaba
abc
abc
abc
bc
*/

C++: #include "stdio.h"
#include<iostream>
#include<string>
#include<algorithm>

#define MAX 1000

using namespace std;

int lcs(string input1, string input2){
  int m = input1.length();
  int n = input2.length(); 

  input1 = " " + input1;
  input2 = " " + input2; 

  //Singleton?
  int c[MAX+1][MAX+1];
    
  for(int i=0;i<=m;i++){
    c[i][0] = 0;
  }
  for(int j=0;j<=n;j++){
    c[0][j] = 0;
  }

  for(int i=1;i<=m;i++){
    for(int j=1;j<=n;j++){
      
      if(input1[i] == input2[j]){
        c[i][j] = c[i-1][j-1] + 1;
      }else{
        c[i][j] = max(c[i-1][j], c[i][j-1]);
      }
    
    }     
  }
  return c[m][n];
}

int main(void){
  
  int N;
  scanf("%d", &N);

  string  input1, input2;

  for(int i=0;i<N;i++){
    cin >> input1;
    cin >> input2;
    printf("%d\n", lcs(input1, input2));
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rp(i,j) for(long long i=0;i<j;i++)
#define rep(i,j,k) for(long long i=j;i<k;i++)
#define mod 1000000007
#define NIL -1
typedef long long ll;
int lcs(string x,string y){
    int dp[1001][1001];
    int maxl=0;
    int X=x.size(),Y=y.size();
    x=" "+x;y=" "+y;
    rp(j,X+1)dp[j][0]=0;
    rp(j,Y+1)dp[0][j]=0;
    rep(j,1,X+1){
        rep(k,1,Y+1){
            if(x[j]==y[k])dp[j][k]=dp[j-1][k-1]+1;
            else dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
        }
    }
    return dp[X][Y];
}
int main(){
    int q;cin>>q;
    rp(i,q){
        string s1,s2;
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <list>
#include <queue>
#include <deque>
#include <algorithm>
#include <numeric>
#include <utility>
#include <complex>
#include <functional>
#include <climits>

using namespace std;


int lcs(string x, string y) {
    int c[1001][1001];
    int n = x.length();
    int m = y.length();
    for(int i = 0; i < n; i++) {
        c[i][0] = 0;
    }
    for(int j = 0; j < m; j++) {
        c[0][j] = 0;
    }
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(x[i-1] == y[j-1]) {
                c[i][j] = c[i-1][j-1] + 1;
            } else {
                c[i][j] = max(c[i][j-1], c[i-1][j]);
            }
        }
    }
    return c[n][m];
}

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

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

std::string x,y;
int lcs[1000][1000];

int max(int k,int l){
  if(k>l){
    return k;
  }else{
    return l;
  }
}

void LCS(int i,int j){
  if(x[i] == y[j]){
    lcs[i][j] = lcs[i-1][j-1]+1;
  }else{
    lcs[i][j] = max(lcs[i-1][j],lcs[i][j-1]);
  }
}

void pre(){
  for(int i=0;i<(int)y.size();i++){
    if(x[0] == y[i]){
      for(int j=i;j<(int)y.size();j++){
	lcs[0][j] = 1;
      }
      break;
    }else{
      lcs[0][i] = 0;
    }
  }

  for(int i=0;i<(int)x.size();i++){
    if(x[i] == y[0]){
      for(int j=i;j<(int)x.size();j++){
	lcs[j][0] = 1;
      }
      break;
    }else{
      lcs[i][0] = 0;
    }
  }

}

int main(){
  int q;
  std::cin >> q;
  for(int k=0;k<q;k++){
    std::cin >> x;
    std::cin >> y;
    int size_x=x.size(),size_y=y.size();
    pre();
    for(int i=1;i<size_x;i++){
      for(int j=1;j<size_y;j++){
	LCS(i,j);
      }
    }

    /*for(int i=0;i<size_x;i++){
      for(int j=0;j<size_y;j++){
	std::cout << lcs[i][j] << ' ';
      }
      std::cout << '\n';
    }
    */
    
    std::cout << lcs[size_x-1][size_y-1] << '\n'; 

  }
  return 0;
}

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


int solve(string x, string y) {
  int lcs[MAX + 1][MAX + 1];
  int xl = x.size();
  int yl = y.size();

  int i, j;
  for (i = 0; i < xl; i++)
    lcs[i][0] = 0;
  for (j = 0; i < yl; j++)
    lcs[0][j] = 0;

  // shift
  x = " " + x;
  y = " " + y;

  int maxlcs = 0;
  for (i = 1; i <= xl; i++) {
    for (j = 1; j <= yl; j++) {
      if (x[i] == y[j]) {
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      } else {
        int a = lcs[i][j - 1];
        int b = lcs[i - 1][j];
        lcs[i][j] = max(a, b);
      }
      maxlcs = max(maxlcs, lcs[i][j]);
    }
  }
  return maxlcs;
}

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