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 <cstdio>
#include <iostream>
#include <string>

using namespace std;

int LCS(string X, string Y) {

	int lcs[X.size() + 1][Y.size() + 1];

	for (int i = 0; i < X.size(); ++i) {
		lcs[0][i] = 0;
	}
	for (int i = 0; i < Y.size(); ++i) {
		lcs[i][0] = 0;
	}

	for (int i = 1; i <= X.size(); ++i) {
		for (int j = 1; j <= Y.size(); ++j) {
			if (X[i - 1] == Y[j - 1]) {
				lcs[i][j] = lcs[i - 1][j - 1] + 1;
			} else if (lcs[i - 1][j] > lcs[i][j - 1]) {
				lcs[i][j] = lcs[i - 1][j];
			} else {
				lcs[i][j] = lcs[i][j - 1];
			}
		}
	}

	return lcs[X.size()][Y.size()];
}

int main() {

	int q;
	scanf("%d", &q);

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

	return 0;

}

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

using namespace std;


int solve(const string& s1, const string& s2) {
    const size_t N = 1000 + 1;
    int A[N][N] = {0};

    auto m = s1.size();
    auto n = s2.size();
    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (s1[i-1] == s2[j-1]) {
                A[i][j] = A[i-1][j-1] + 1;
            } else {
                A[i][j] = max(A[i][j-1], A[i-1][j]);
            }
        }
    }
    return A[m][n];
}

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

    string s1, s2;
    for (int i = 0; i < q; ++i) {
        cin >> s1 >> s2;
        cout << solve(s1, s2) << endl;
    }

    return 0;
}

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

using namespace std;

#if 0

以下のURLを参照。参照した箇所は、a = ' ' + a;のように空白をいれていることで文字のサイズを合わせていることである。
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2065462#1

サンプルインプットでは正しい出力を得られているようにみられているが、なぜかwrong answerが出力されてしまう。
#endif

int n;
string a, b;



int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1051][1051];
    cin >> a;
    cin >> b;
    //a = ' ' + a;
    //b = ' ' + b;
    for(int i = 0; i <= a.size(); ++i){
      for(int j = 0; j <= b.size(); ++j){
        if(j <= 0 || i <= 0){
          longest[i][j] = 0;
        }
        else if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        
      }
    }
    //cout << a[1] << endl;
    cout << longest[a.size()][b.size()] << endl;
  }
}

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

using namespace std;
static const int N=1000;
int lcs(string X, string Y){
    int m=X.size();
    int n=Y.size();
    int c[N+1][N+1];
    int maxl=0;
    X=' '+X;
    Y=' '+Y;
    int i,j;
    for(i=1;i<=m;i++)c[i][0]=0;
    for(j=1;j<=n;j++)c[0][j]=0;

    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][j-1],c[i-1][j]);
            }
            maxl=max(maxl,c[i][j]);
        }
    }
    return maxl;
}
int main()
{
    int n;
    string s1,s2;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }

    return 0;
}

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


int main()
{
    int q;
    cin >> q;
    for(int i = 0; i < q; i++){
        string x, y;
        cin >> x >> y;
        int c[1001][1001];
        for(int j = 0; j <= (int)x.size(); j++) c[j][0] = 0;
        for(int j = 0; j <= (int)y.size(); j++) c[0][j] = 0;
        for(int j = 1; j <= (int)x.size(); j++){
            for(int k = 1; k <= (int)y.size(); k++){
                if(x[j - 1] == y[k - 1]) c[j][k] = c[j - 1][k - 1] + 1;
                else c[j][k] = max(c[j - 1][k],c[j][k - 1]);
                
            
            }
        
        }
        cout << c[x.size()][y.size()] << endl;
        
        
        
    }
    
    
    return 0;
}

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

#define MAX 1000

int lcs(string x, string y) {
  int c[MAX + 1][MAX + 1];
  int m = x.size();
  int n = y.size();
  int maxl = 0;
  x = ' ' + x;
  y = ' ' + y;
  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() {
  string s1, s2;
  int q;
  cin >> q;

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

  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
static const int N = 1000;

int lcs(string x, string y){
  int c[N + 1][N + 1], maxl = 0, i, j;
  int m = x.size();
  int n = y.size();

  x = ' ' + x;
  y = ' ' + y;
 
  for(i = 1; i <= m; i++) c[i][0] = 0;
  for(i = 0; i <= n; i++) c[0][i] = 0;

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

int main(){
  string x, y;
  int q, i;

  cin >> q;
  
  for(i = 0; i < q; i++){
    cin >> x >> y;
    cout << lcs(x, y) << endl;
  }
  return 0;
}

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

int lcs(string X, string Y) {
    int c[1001][1001];
    int m = X.size();
    int n = Y.size();
    X = ' ' + X;
    Y = ' ' + Y;
    int maxl = 0;
    for (int i = 0; i < m + 1; i++) c[i][0] = 0;
    for (int j = 1; j < n + 1; j++) c[0][j] = 0;
    
    for (int i = 1; i < m + 1; i++) {
        for (int j = 1; j < n + 1; 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() {
    string s1, s2;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N = 1000;

int lcs(string X, string Y){
	int c[N + 1][N + 1];
	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(){
	string s1,s2;
	int n; cin >> n;
	for(int i=0;i<n;i++){
		cin >> s1 >> s2;
		cout << lcs(s1,s2) << endl;
	}
	return 0;
}