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 <cstdio>
#include <iostream>
#include <string>
using namespace std;

int LCS(string a, string b)
{
    int ret;
    int i, j;
    int len1, len2;
    int result[1001][1001] = {0};

    len1 = a.length();
    len2 = b.length();

    for (i = 1; i < len1; i++) {
        for (j = 1; j < len2; j++) {
            if (a[i] == b[j]) {
                result[i][j] = result[i - 1][j - 1] + 1;
            }
            else {
                result[i][j] = max(result[i][j - 1], result[i - 1][j]);
            }
        }
    }

    return result[len1 - 1][len2 - 1];
}

int main(void)
{
    int i;
    int num;
    int ret;
    string a, b;

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        cin >> a;
        cin >> b;
        ret = LCS(a, b);
        printf("%d\n", ret);
    }

    return 0;
}

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

#define MAX 1000

using namespace std;

int lcs(string s1, string s2) {
    int C[MAX+1][MAX+1];
    int m = s1.size();
    int n = s2.size();
    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 (s1[i-1] == s2[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[m][n];
}

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

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int LCS(char s[],char t[])
{
    int c[50][50];
    int i,j;
    int n = strlen(s);
    int m = strlen(t);
    int maxx = 0;
    for(i=0;i<n;i++)
        c[i][0] = 0;
    for(j=0;j<m;j++)
        c[0][j] = 0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<m;j++)
        {
            if(s[i] == t[j])
                c[i][j] = c[i-1][j-1]+1;
            else
                c[i][j] = max(c[i-1][j],c[i][j-1]);
            maxx = max(maxx,c[i][j]);
        }
    }
    return maxx;
}
int main()
{
    char s[50],t[50];
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",s);
        scanf("%s",t);
        int p = LCS(s,t);
        printf("%d\n",p);
    }

    return 0;
}

C++: 
#include<bits/stdc++.h>

using namespace std;

const int maxn=1e3+5;

int dp[maxn][maxn];
string s1,s2;

void lcs()
{
    int len1=s1.size();
    int len2=s2.size();
    s1=" "+s1;
    s2=" "+s2;
    for(int i=1;i<=len1;i++){
        for(int j=1;j<=len2;j++){
            if(s1[i]==s2[j]){
                dp[i][j]=dp[i-1][j-1]+1;
            }else{
                dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }
        }
    }
//    for(int i=0;i<=len1;i++){
//        for(int j=0;j<=len2;j++){
//            cout<<dp[i][j]<<" ";
//        }
//        cout<<endl;
//    }
    cout<<dp[len1][len2]<<endl;
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        lcs();
    }
    return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <algorithm>
using namespace std;
const int num = 1001;
int lg[num + 1][num + 1];
int main() {
	int n, m;
	char s[num], t[num];
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> s[i];
	for (int i = 0; i < m; i++) cin >> t[i];
	for (int i = 0; i < n; i++) lg[i][0] = 0;
	for (int i = 0; i < m; i++) lg[0][i] = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < m; j++) {
			if (s[i] == t[j]) lg[i + 1][j + 1] = lg[i][j] + 1;
			else lg[i + 1][j + 1] = max(lg[i][j + 1], lg[i + 1][j]);
		}
	cout << lg[n][m] << endl;
	return 0;
}

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

using namespace std;

int main()
{
    int q;
    cin >> q;
    for(int count = 0; count < q; count++){
        string str_x, str_y;
        cin >> str_x;
        cin >> str_y;
        int length_x, length_y;
        length_x = str_x.size();
        length_y = str_y.size();
        
        vector<vector<int>> common;
        common.resize(length_x+1);
        for(int x = 0; x <= length_x; x++){
            common[x].resize(length_y+1);
        }
        
        for(int x = 0; x <= length_x; x++){
            common[x][0] = 0;
        }
        for(int y = 0; y <= length_y; y++){
            common[0][y] = 0;
        }
        for(int x = 1; x <= length_x; x++){
            for(int y = 1; y <= length_y; y++){
                if(str_x[x] == str_y[y]){
                    common[x][y] = common[x-1][y-1] + 1;
                } else {
                    if(common[x][y-1] > common[x-1][y]){
                        common[x][y] = common[x][y-1];
                    } else {
                        common[x][y] = common[x-1][y];
                    }
                }
            }
        }
        cout << common[length_x][length_y] << endl;
    }
    return 0;
}

C++: #include "iostream"
#include "string.h"

using namespace std;

int L[1001][1001];

int lcs( char s1[], char s2[], int l1, int l2 ) {

	if (L[l1][l2] != 0)return L[l1][l2];

	if (l1 == 0 || l2 == 0) return 0;
	else if (s1[l1-1] == s2[l2-1]) L[l1][l2] = lcs(s1, s2, l1 - 1, l2 - 1) + 1;
	else {
		int lsc1 = lcs(s1, s2, l1 - 1, l2);
		int lsc2 = lcs(s1, s2, l1, l2 - 1);

		if (lsc1 > lsc2) L[l1][l2] = lsc1;
		else L[l1][l2] = lsc2;
	}

	return L[l1][l2];
}

int main() {

	int n;
	char s1[1000], s2[1000];

	cin >> n;

	int ans[150];

	for (int i = 0; i < n; i++) {
		cin >> s1;
		cin >> s2;

		int l1 = strlen(s1), l2 = strlen(s2);

		ans[i] = lcs(s1, s2, l1, l2);

		for (int j = 0; j <= l1; j++) {
			for (int k = 0; k <= l2; k++) {
				L[j][k] = 0;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

int lcs(string X, string Y) {
  int memo[1010][1010];
  int maxl = 0;

  // 初期化、境界条件を考慮して場合分け
  for (int i = 0; i < X.size(); i++) {
    if (X[i] == Y[0]) {
      memo[i][0] = 1;
    } else {
      memo[i][0] = 0;
    }
  }

  for (int j = 0; j < Y.size(); j++) {
    if (X[0] == Y[j]) {
      memo[0][j] = 1;
    } else {
      memo[0][j] = 0;
    }
  }

  for (int i = 0; i < X.size(); i++) {
    for (int j = 0; j < Y.size(); j++) {
      if (X[i] == Y[j]) {
        memo[i][j] = memo[i-1][j-1] + 1;
      } else {
        memo[i][j] = max(memo[i-1][j], memo[i][j-1]);
      }
      maxl = max(maxl, memo[i][j]);
      //cout << i << "," << j << "," << X[i] << "," << Y[j] << "," << maxl << endl;
    }
  }
  return maxl;
}

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