Go: package main

import "fmt"

func initialize_grid(width int,height int)[][]int{
	var grid [][]int
	for i:=0;i<height+1;i++{
		var grid_row  []int
		for j:=0;j<width+1;j++{
			grid_row = append(grid_row,0)
		}
		grid = append(grid,grid_row[:])
	}
	return grid
}

func ma(x int,y int)int{
	if x >= y{
		return x
	}else{
		return y
	}
}

func main()  {
	//Take input
	var num int
	var st1,st2 string
	var string_pairs [][]string
	fmt.Scan(&num)
	for i:=0;i<num;i++{
		var string_pair []string
		fmt.Scan(&st1)
		fmt.Scan(&st2)
		string_pair = append(string_pair,st1,st2)
		string_pairs = append(string_pairs,string_pair)
	}
	//DP
	//initialize grid
	for i:=0;i<num;i++{
		string_1 := string_pairs[i][0]
		string_2 := string_pairs[i][1]

		dp := initialize_grid(len(string_2),len(string_1))

		for j:=1;j<len(string_1)+1;j++{
			for k:=1;k<len(string_2)+1;k++{
				if string_1[j-1] == string_2[k-1]{
					dp[j][k] = dp[j-1][k-1] + 1
				}else{
					dp[j][k] = ma(dp[j-1][k],dp[j][k-1])
				}
			}
		}
		fmt.Println(dp[len(string_1)][len(string_2)])
	}
}

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

C++: #include <stdio.h>
#include <string.h>
#define N 1010
char a[N];
char b[N];
int dp[N][N];
//int LCS(int i, int j){
//    if(i == -1 || j == -1) return 0;
//    if(a[i] == b[j]) return LCS(i-1, j-1) + 1;
//    else return LCS(i-1, j) > LCS(i, j-1) ? LCS(i-1, j) : LCS(i, j-1);
//}
int main(){
    int k;
    scanf("%d", &k);
    while(k--){
        scanf("%s", a);
        scanf("%s", b);
        int n = strlen(a);
        int m = strlen(b);
        for(int i = n-1; i >= 0; i--) a[i+1] = a[i];
        for(int i = m-1; i >= 0; i--) b[i+1] = b[i];
        int max = n > m ? n : m;
        for(int i = 0; i <= max; i++) dp[0][i] = dp[i][0] = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1] + 1;
                else {
                    dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
                }
            }
        }
        printf("%d\n", dp[n][m]);
        //printf("%d\n", LCS(strlen(a)-1, strlen(b)-1));
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

const int MAX_L=1000;//?????????????????§??????

vector<vector<int> > DP(MAX_L, vector<int>(MAX_L, -1));
string A;//?????????????????????????????????
string B;

//A???a???????????¨????????\??????B???b???????????¨????????\??????????????§????????§?????¨????????????????????????????????????
//m?????????????????????????????±??????????????????
int dev(int a, int b){
	if(DP[a][b]<0){
		int maxi= 0;
		//???????????????????????§??????
		for(int c='a'; c<='z'; ++c){
			int posa = A.find(c,a);
			int posb = B.find(c,b);
			if(posa>=0 && posb>=0){
				int tmp = dev(posa+1,posb+1);
				maxi = max(maxi, tmp+1);
			}
		}
		DP[a][b]=maxi;
	}
	return DP[a][b];
}

int main(){
	int q;
	cin >> q;
	vector<int> ans(q);
	REP(i,q){
		REP(k,MAX_L){
			REP(l,MAX_L){
				DP[k][l] = -1;
			}
		}
		cin>>A>>B;
		ans[i]=dev(0,0);
	}
	REP(i,q){
		cout<<ans[i]<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (int)(n); i++)


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

  rep(i,q){
      string X,Y;
      cin >> X >> Y;
      int xl,yl;
      xl = X.size();
      yl = Y.size();

      vector<vector<int>> dp(xl+1,vector<int>(yl+1));
      for(int i = 0;i < xl;i++){
          for(int j = 0;j < yl;j++){
              if (X.at(i) == Y.at(j)){
                  dp.at(i+1).at(j+1) = max(dp.at(i).at(j)+1,dp.at(i+1).at(j+1));
              }
              dp.at(i+1).at(j+1) = max(dp.at(i+1).at(j+1),dp.at(i+1).at(j));
              dp.at(i+1).at(j+1) = max(dp.at(i+1).at(j+1),dp.at(i).at(j+1));
          }
      }
      cout << dp.at(xl).at(yl) << endl;


  }
  
}

C++: #include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize "O3"
#define rep(i,a,n) for(int i=a; i<n; i++)
typedef long long ll;
typedef pair<int,int> P;
const ll MOD = (int)1e9+7;
const ll INF = MOD*MOD;
const ll powll = 1LL<<1;


int main() {
    int t;
    cin >> t;
    rep(i,0,t){
        string s1,s2;
        cin >> s1 >> s2;
        int n1 = s1.size();
        int n2 = s2.size();
        int dp[n1+1][n2+1];
        rep(j,0,n1+1){
            rep(k,0,n2+1){
                dp[j][k] = 0;
            }
        }
        rep(j,0,n1){
            rep(k,0,n2){
                if(s1[j] == s2[k]){
                    dp[j+1][k+1] = max(dp[j+1][k], dp[j][k] + 1);
                }else{
                    dp[j+1][k+1] = max(dp[j+1][k], dp[j][k+1]);
                }
            }
        }
        // rep(j,0,n1+1){
        //     rep(k,0,n2+1){
        //         cout << dp[j][k] << " ";
        //     }
        //     cout << "\n";
        // }
        cout << dp[n1][n2] << "\n";
    }
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
// Dynamic Programming - Longest Common Subsequence

#define SUBMIT

#include <utility>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <limits>
#include <numeric>
#include <functional>
#include <set>
#include <cmath>
#include <cstring>
#include <climits>
#include <memory>

using namespace std;
using ui64 = unsigned long long;
using i64 = long long;

const ui64 MAX_N = 1000;
ui64 dp[MAX_N + 1][MAX_N + 1];

ui64 compute_lcs(string x, string y) {
    memset(dp, 0, sizeof(dp));

    for (int i = 0; i < x.length(); ++i) {
        for (int j = 0; j < y.length(); ++j) {
            if (x[i] == y[j]) dp[i + 1][j + 1] = dp[i][j] + 1;
            else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
    }

    return dp[x.length()][y.length()];
}

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

    string x, y;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        cout << compute_lcs(x, y) << endl;
    }

    return 0;
}