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

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int q,i,j,k,l1,l2,m,n;
    string X,Y;
    cin>>q;
    for(i=0; i<q; i++)
    {
        cin>>X>>Y;
        l1=X.length()+1;
        l2=Y.length()+1;
        int arr[l2][l1];
        for(k=0; k<l1; k++)
        {
            arr[0][k]=0;
        }
        for(k=0; k<l2; k++)
        {
            arr[k][0]=0;
        }
        for(k=1; k<l2; k++)
        {
            for(j=1; j<l1; j++)
            {
                if(Y[k-1]==X[j-1])
                {
                    arr[k][j]=1+arr[k-1][j-1];
                }
                else
                {
                    m=arr[k-1][j];
                    n=arr[k][j-1];
                    arr[k][j]=max(m,n);
                }
            }
        }
        cout<<arr[l2-1][l1-1]<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define repp(i, m, n) for(int i = m; i < n; i++)
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll,ll> pal;
int q;
string x, y;
int n, m;
int dp[1001][1001];
int main()
{
	cin >> q;
	for(int i=0; i<q; i++)
	{
		for(int j=0; j<1001; j++)
		{
			for(int k=0; k<1001; k++)
			{
				dp[j][k]=0;
			}
		}
		cin >> x >> y;
		n=x.size();
		m=y.size();
		for(int j=1; j<=n; j++)
		{
			for(int k=1; k<=m; k++)
			{
				if(x[j-1]==y[k-1])
				{
					dp[j][k]=dp[j-1][k-1]+1;
				}
				else
				{
					dp[j][k]=max(dp[j-1][k], dp[j][k-1]);
				}
			}
		}
		cout << dp[n][m] << endl;
	}
	return 0;
}