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 <iostream>
#include <string>
#include <algorithm>
    using namespace std;
int lcs(string x,string y){
    int c[1010][1010];
    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(){
    int q;
    string x;
    string y;
    scanf("%d",&q);
    for (int i=0; i<q; i++) {
        cin>>x>>y;
        cout<<lcs(x, y)<<endl;
    }
    return 0;
}

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

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

// 最長共通部分列 ... len(s) == m, len(t) == n とすると, O(mn)
int lcs(std::string s, std::string t) {
  std::vector<std::vector<int> > dp(s.size() + 1, std::vector<int>(t.size() + 1, 0));

  for (int i = 1; i <= s.size(); ++i) {
    for (int j = 1; j <= t.size(); ++j) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
      else if (dp[i - 1][j] >= dp[j][i - 1]) {
        dp[i][j] = dp[i - 1][j];
      }
      else {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }

  return dp[s.size()][t.size()];
}

int main() {
  std::string s, t;

  while (cin >> s >> t) {
    cout << lcs(s, t) << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int func2(vector<vector<int> > &a, string x, string y)
{
    int m = x.size(), n = y.size();
    x = ' ' + x, y = ' ' + y;
    for (int i = 0; i <= m; ++i)
        a[i][0] = 0;
    for (int j = 1; j <= n; ++j)
        a[0][j] = 0;
    for (int i = 1; i <= m; ++i)
    {
        for (int j = 1; j <= n; ++j)
        {
            if (x[i] == y[j])
                a[i][j] = a[i - 1][j - 1] + 1;
            else
                a[i][j] = max(a[i - 1][j], a[i][j - 1]);
        }
    }
    return a[m][n];
}

int main()
{
    int q;
    cin >> q;
    int ans[q + 1] = {};
    for (int i = 0; i < q; ++i)
    {
        string x, y;
        cin >> x >> y;
        vector<vector<int> > a(1001, vector<int>(1001));
        ans[i] = func2(a, x, y);
    }
    for (int i = 0; i < q; ++i)
    {
        printf("%d\n", ans[i]);
    }
}

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

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

	while (n--)
	{
		int memo1[1005] = {};
		int memo2[1005] = {};
		string a, b;
		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<string>
#include<cstring>
#include<algorithm>

using namespace std;

int DP[1001][1001];

int main(){
    int T;
    cin >> T;
    for (int t = 0; t < T; t++) {
        char X[1002],Y[1002];
        scanf("%s%s",X,Y);
        
        int y = strlen(Y);
        int x = strlen(X);
        
        for (int i = 0; i <= y; i++) DP[i][0] = 0;
        for (int i = 0; i <= x; i++) DP[0][i] = 0;
        
        for (int r = 1; r <= y; r++) {
            for (int c = 1; c <= x; c++) {
                if (Y[r-1] == X[c-1]) DP[r][c] = DP[r-1][c-1]+1;
                else DP[r][c] = max(DP[r-1][c],DP[r][c-1]);
            }
        }
        cout << DP[y][x] << endl;
        
    }
    
    
    return 0;
}

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

int lcs(string str1, string str2)
{
	int len[1001][1001] = { 0 };

	int len1 = str1.size();
	int len2 = str2.size();
	str1 = ' ' + str1;
	str2 = ' ' + str2;

	for (int i = 0; i <= len1; i++)
		for (int j = 0; j <= len2; j++) len[i][l] = 0;

	for (int i = 1; i <= len1; i++)
	{
		for (int j = 1; j <= len2; j++)
		{
			if (str1[i] == str2[j]) len[i][j] = len[i - 1][j - 1] + 1;
			else len[i][j] = max(len[i - 1][j], len[i][j - 1]);
		}
	}
	return len[len1][len2];
}

int main()
{
	int n;
	string str1, str2;

	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> str1 >> str2;
		cout << lcs(str1, str2) << endl;
	}

	return 0;
}

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] == t[j]) {
        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<vector>
#include<string>
using namespace std;
string s1,s2;
int solve(int l1,int l2){
  //  cout << l1 << ' ' << l2 << endl;
  if(l1 <= 0 or l2 <= 0)return 0;
  if(s1[l1] == s2[l2])return solve(l1-1,l2-1) + 1;
  return max(solve(l1,l2-1), solve(l1-1,l2));
}

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

C++: #include<stdio.h>
#include<string.h>

#define N 1000
#define S 150

int LCSLength(char *, char *);

int main(){
  int q, i;
  char X[N];
  char Y[N];
  int LCS[S];
  scanf("%d", &q);
  for(i=0; i<q; i++){
    scanf("%s", X);
    scanf("%s", Y);
    LCS[i]=LCSLength(X, Y);
  }

  for(i=0; i<q; i++){
    printf("%d\n", LCS[i]);
  }

  return 0;
}//main

int LCSLength(char *X, char *Y){
  int m, n, i, j;
  int c[N+1][N+1];
  m = strlen(X);
  n = strlen(Y);
  for(i=1; i<=m; i++){
    c[i][0]= 0;
  }
  for(j=0; j<=n; j++){
    c[0][j]= 0;
  }

  for(i=1; i<=m; i++){
   for(j=1; j<=n; j++){
     if(X[i-1]==Y[j-1]) c[i][j]=c[i-1][j-1]+1;
     else if(c[i-1][j]>=c[i][j-1]) c[i][j]=c[i-1][j];
     else c[i][j]=c[i][j-1];
    }
   }
   return c[m][n];
  }