Go: package main

import "fmt"

func main() {
	var n int
	fmt.Scan(&n)

	for q := 0; q < n; q++ {
		var s1, s2 string
		fmt.Scan(&s1, &s2)
		s1, s2 = " " + s1, " " + s2

		dp := make([][]int, len(s1))
		for i := range dp {
			dp[i] = make([]int, len(s2))
		}

		max := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); 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])
				}
				max = Max(max, dp[i][j])
			}
		}

		fmt.Println(max)
	}
}

func Max(n ...int) int {
	max := n[0]
	for _, v := range n[1:] {
		if v > max {
			max = v
		}
	}
	return max
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>

using namespace std;

string s1,s2;

int L(int i,int j){
	if(i<0||j<0)return 0;
	else if(s1.at(i)==s2.at(j))return 1+L(i-1,j-1);
	else{
		int a=L(i,j-1);
		int b=L(i-1,j);
		return a>b?a:b;
	}
}

int main(){
	int q;
	cin>>q;
	for(int i=0; i<q; i++){
		cin>>s1>>s2;
		cout<<L(s1.size()-1,s2.size()-1)<<endl;
	}
}

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

int q;
string X, Y;

int dp[1000][1000];

int solve(int idx_x, int idx_y) {
  if (idx_x < 0 || idx_y < 0)
    return 0;

  if (dp[idx_x][idx_y] != -1)
    return dp[idx_x][idx_y];

  int ret;
  
  if (X[idx_x] == Y[idx_y]) {
    ret = solve(idx_x - 1, idx_y - 1) + 1;
  } else {
    ret = solve(idx_x - 1, idx_y);
    ret = max(solve(idx_x, idx_y - 1), ret);
  }
  
  return dp[idx_x][idx_y] = ret;
}

int main(void) {
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X >> Y;
    memset(dp, -1, sizeof(dp));
    cout << solve(X.size()-1, Y.size()-1) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int q;
char X[1000], Y[1000];
int dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
        scanf("%s %s", X, Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen; ++x) {
            for (int y = 0; y < ylen; ++y) {
                if (X[x] == Y[y]) {
                    dp[x + 1][y + 1] = dp[x][y] + 1;
                }
                else {
                    if (dp[x + 1][y] > dp[x][y + 1]) {
                        dp[x + 1][y + 1] = dp[x + 1][y];
                    }
                    else {
                        dp[x + 1][y + 1] = dp[x][y + 1];
                    }
                }
            }
        }
        printf("%d\n", dp[xlen][ylen]);
//        memset(dp, 0, sizeof(int)*1001*1001);
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int calcAns(string s, string t) {
    int r = s.size() + 1;
    int c = t.size() + 1;

    int dp[r][c];
    rep(i, r)rep(j,c) dp[i][j] = 0;

    for(int i = 1; i < r; i++) {
        for(int j = 1; j < c; j++) {
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            if(s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
        }
    }
    return dp[r-1][c-1];
}

int main(void)
{
    int q;
    cin >> q;

    rep(i, q) {
        string s, t;
        cin >> s >> t;
        int ans = calcAns(s, t);
        cout << ans << endl;
    }
    return 0;
}

C++: #ifdef DEBUG
#include "../utils.cpp"
#endif
#include <bits/stdc++.h>
/*
Problem
最長共通部分列

 
Category
DP
 
URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C&lang=ja

考察:
dp[X][Y] := Xに対するYのLCSを作るのに必要なXの部分文字列のindexの最小値
--> LCSのサイズの情報がないのでダメ。

dp[X][Y] := XとYで作れる LCS の長さ
--> 素直な定義。でも漸化式が作れるのか？
dp[0][Y] = 0
dp[i][j] = X[i] == Y[j] ? dp[i-1][j-1] + 1 : max(dp[i][j-1], dp[i-1][j])



Date: 2020/04/18
*/
 

using namespace std;
using ll = int64_t;
#define int int64_t
#define INF 10e9

 
/* ==== declarations ====== */
// functions
void solve();
void getInput();
 
// global variables
int Q;
vector<vector<int>> dp;
vector<int> V;
string X, Y;

/* ======================== */

void getInput() {
    cin>>X;
    cin>>Y;
}
 
 
void solve() {
    dp = vector<vector<int>>(X.size(), vector<int>(Y.size(), 0));

    for (int i=0; i<X.size(); ++i) {
        for (int j=0; j<Y.size(); ++j) {
            int a = (i>0 and j>0) ? dp[i-1][j-1] : 0;
            int b = (j>0) ? dp[i][j-1] : 0;
            int c = (i>0) ? dp[i-1][j] : 0;
            if (X[i] == Y[j]) {
                dp[i][j] = max(a + 1, max(b, c));
            }
            else {
                dp[i][j] = max(b, c);
            }
        }
    }
    // print(dp);
    cout<<dp[X.size()-1][Y.size()-1]<<endl;
}
 
 
 
signed main(){
    cin>>Q;
    for(int i=0; i<Q; ++i) {
        getInput();
        solve();
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repR(i, n) for(int i = (n) - 1; i > -1; i--)
#define rep1(i, n) for(int i = 1; i < (int)(n + 1); i++)
#define rep1R(i, n) for(int i = (n); i > 0; i--)
using namespace std;

int q;
int dp[1010][1010];

int lcs(string& X, string& Y) {
  int m = X.size(), n = Y.size();
  X = ' ' + X, Y = ' ' + Y;
  int result = 0;
  rep1(i, m) dp[i][0] = 0;
  rep1(j, n) dp[0][j] = 0;
  rep1(i, m) rep1(j, n) {
    if (X[i] == Y[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
    else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
    result = max(result, dp[i][j]);
  }
  return result;
}

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

C++: #include <iostream>
#include <algorithm>
#define LENGTH 1000

using namespace std;

int LCSLength(string X, string Y){
    int m = X.size();
    int n = Y.size();
    int c[LENGTH+1][LENGTH+1];
    int maxLength = 0;
    for(int i = 1; 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 if(c[i-1][j] >= c[i][j-1]) c[i][j] = c[i-1][j];
            else c[i][j] = c[i][j-1];
            maxLength = max(maxLength, c[i][j]);
        }
    }
    return maxLength;
}

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

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<ll, P> E;
#define MOD (1000000007ll)
#define l_ength size
#define PI 3.14159265358979
 
void mul_mod(ll& a, ll b){
    a *= b;
    a %= MOD;
}
 
void add_mod(ll& a, ll b){
    b += MOD;
    a += b;
    a %= MOD;
}

int main(void){
    int q,k,dp[1234][1234],i,j,n,m;
    string x,y;
    cin >> q;
    for(k=0; k<q; ++k){
        fill(dp[0],dp[1234],0);
        cin >> x >> y;
        n = x.l_ength();
        m = y.l_ength();
        for(i=0; i<n; ++i){
            for(j=0; j<m; ++j){
                dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
                if(x[i] == y[j]){
                    dp[i+1][j+1] = max(dp[i][j]+1,dp[i+1][j+1]);
                }
            }
        }
        cout << dp[n][m] << endl;
    }
    return 0;
}