Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for 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
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main(){
  int q;
  cin >> q;
  rep(i, q){
    string a, b;
    cin >> a >> b;
    vector<vector<int>> dp(b.size()+1, vector<int>(a.size()+1));
    rep(i, b.size()+1){
      rep(j, a.size()+1){
        if(i == 0 || j == 0){
          dp.at(i).at(j) = 0;
          continue;
        }
        if(a.at(j-1) == b.at(i-1)) dp.at(i).at(j) = dp.at(i-1).at(j-1) + 1;
        else dp.at(i).at(j) = max(dp.at(i-1).at(j), dp.at(i).at(j-1));
      }
    }
    cout << dp.at(b.size()).at(a.size()) << endl;
  }
  return 0;
}

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

int main()
{
	int n;
	string a, b;

	while (n--)
	{
		int memo1[1005] = {};
		int memo2[1005] = {};
		cin >> a >> b;

		for (int i = 1; i <= a.size(); i++)
		{
			memo2[0] = 0;
			for (int j = 1; j <= b.size(); j++)
			{
				if (a[i] == b[j])
					memo2[j] = max({ memo2[j - 1], memo1[j], memo1[j - 1] + 1 });
				else
					memo2[j] = max(memo2[j - 1], memo1[j]);
			}
			swap(memo1, memo2);
		}

		cout << memo1[b.size()] << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string.h>

using namespace std;


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

  for(int i = 0; i < n; i++){
    char x[1000], y[1000];
    cin >> x >> y;
    int c[strlen(x) + 2][strlen(y) + 2];
    for(int j = 0; j <= strlen(x);j++) c[j][0] = 0;
    for(int j = 0; j <= strlen(y);j++) c[0][j] = 0;


    for(int j = 1; j <= strlen(x); j++){
      for(int k = 1; k <= strlen(y); k++){
        if(x[j - 1] == y[k - 1]) c[j][k] = c[j - 1][k - 1] + 1;
        else c[j][k] = max(c[j][k - 1], c[j - 1][k]);
      }
    }
    cout << c[strlen(x)][strlen(y)] << endl;

  }

  return 0;
}

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

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 ans=0;
  X= ' '+X;
  Y= ' '+Y;
  for(int i;i<=m;i++)c[i][0]=0;
  for(int j;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]);
      }
      ans=max(ans,c[i][j]);
    }
  
  }
  return ans;
}

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<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define MAX 1005

int table[MAX][MAX];

int LCS(string s, string t);

int main(){
    int q; cin >> q;
    for(int i = 0; i < q; i++){
        string s,t; cin >> s >> t;
        cout << LCS(s, t) << endl;
    }
    return 0;
}

int LCS(string s, string t){
    int slen = s.length(), tlen = t.length();

    for(int i = 0; i <= slen; i++) table[0][i] = 0;
    for(int i = 0; i <= tlen; i++) table[i][0] = 0;
    for(int i = 0; i < slen; i++){
        for(int j = 0; j < tlen; j++){
            if(s[i] == t[j]){
                table[i+1][j+1] = table[i][j] +1;
            }else{
                table[i+1][j+1] = max(table[i][j+1], table[i+1][j]);
            }
        }
    }
    return table[slen][tlen];

}

C++: // Longest Common Subsequence

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
#define MAX 1001
int lcs[MAX][MAX] = {};
string x,y;

void make_lcs() {
	for (unsigned int i=0;i<=x.length();i++) {
	for (unsigned int j=0;j<=y.length();j++) {
	if (i!=0 && j!=0) {
	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]);
	}
	}
	}
	}
}

int main() {
	int n;
	cin >> n;
	for (int k=0;k<n;k++) {
	cin >> x >> y;
	make_lcs();
	cout << lcs[x.length()][y.length()] << endl;
	}
	return 0;
}

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

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

string str1, str2;
int dp[1001][1001];
int k;

int solve(int i, int j)
{
  if(dp[i][j] > 0)
    return dp[i][j];
  if(i >= str1.size() || j >= str2.size()){
    if(i < str1.size()){
      return solve(i + 1, k);
    }else{
      return 0;
    }
  }
  if(str1[i] == str2[j]){
    k = j + 1;
    int n, m = 0;
    for(int l = i + 1; l < str1.size(); l++){
      n = solve(l, j + 1);
      m = (n > m) ? n : m;
    }
    dp[i][j] = m + 1;
    return dp[i][j];
  }else{
    return solve(i, j + 1);
  }
}
  
int main()
{
  int q;
  cin >> q;
  cin.ignore();
  REP(i, q){
    getline(cin, str1);
    getline(cin, str2);
    if(str1.size() < str2.size())
      swap(str1, str2);
    fill(&dp[0][0], &dp[1001][0], -1);
    cout << solve(0, 0) << endl;
  }
}

C++: #include<iostream>
#include<stdio.h>
#include<string.h>
#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=0;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;
}