Go: package main

import (
	"bufio"
	"os"
	"fmt"
	"strconv"
)

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>

using namespace std;

int n;
string x,y;

vector<vector<int> > q(1001,vector<int>(1001,-1));

int search(int i,int j){
  int ans;
  if(i<0 || j<0) return 0;
  if(q[i][j] != -1) return q[i][j];
  if( x[i] == y[j] ){
    ans=max( {search(i-1,j-1)+1,search(i-1,j),search(i,j-1)} );
    q[i][j]=ans;
    return ans;
  }
  else{
    ans=max( search(i-1,j),search(i,j-1) );
    q[i][j]=ans;
    return ans;
  }

}


int main(void){

  cin>>n;

  for(int i=0;i<n;i++){
    cin>>x;
    cin>>y;

    for(int ix=0;ix<1001;ix++){
      for(int iy=0;iy<1001;iy++){
        q[ix][iy]=-1;
      }
    }

    search( x.size(),y.size() );
    cout << q[x.size()-1][y.size()-1] << endl;
  }



  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<limits.h>
using namespace std;
static const int n=1000;
static const int m=1000;

int lcs(string X,string Y){
	int i,j,c[m+1][n+1];
	int m=X.size();
	int n=Y.size();
    
	for (i=0;i<m+1;i++) c[i][0]=i;
	for (j=0;j<n+1;j++) c[0][j]=j;
	for (i=1;i<m+1;i++){
		for (j=1;j<n+1;j++){
			int min=c[i-1][j]+1;
			if (min>c[i][j-1]+1)
				min=c[i][j-1]+1;
            if ((X[i-1]==Y[j-1])&&(min>c[i-1][j-1]+1))
				min=c[i-1][j-1]+1;
			c[i][j]=min;
		}
	}
	return m+n-c[m][n];
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	string s1,s2;
	int m,i;	cin >> m;
	
	for (i=0;i<m;i++){
	cin >> s1 >> s2;
	cout << lcs(s1,s2) << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
int main()
{
  int n;
  int an[1000];
  int l=0;
  std::cin>>n;
  std::string x;
  std::string y;
  int ans[1001][1001];
  int j,k;
  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<=1000;j++){
      for(int k=0;k<=1000;k++){
	ans[j][k]=0;
      }
    }
    for(int j=0;j<=x.size();j++){
      for(int k=0;k<=y.size();k++){
	if(x[j]==y[k]){
	  ans[j][k]=ans[j-1][k-1]+1;
	}else{
	  if(ans[j][k-1]>ans[j-1][k]){
	    ans[j][k]=ans[j][k-1];
	  }else{
	    ans[j][k]=ans[j-1][k];
	  }
	}
      }
    }
    std::cout<<ans[x.size()-1][y.size()-1]<<'\n';  
  }
  return 0;
}

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

int main() {
  int q; cin >> q;
  string A, B; cin >> A >> B;
  for (int i=0; i<q; i++) {
    vector<vector<int>> LCS(A.length()+1, vector<int>(B.length()+1,0));
    for (int j=1; j<=A.length(); j++) {
      for (int k=1; k<=B.length(); k++) {
        if(A[j]==B[k]) LCS[j][k]=LCS[j-1][k-1]+1;
        else LCS[j][k]=max(LCS[j-1][k],LCS[j][k-1]);
      }
    }
    cout << LCS[A.length()][B.length()] << "\n";
  }
}

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

#define N 1000

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

int main(){
	int n;
	string X,Y;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> X >> Y;
		printf("%d\n",lcs(X,Y));
	}
	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=0;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;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int LCS[1000][1000];

void solve(string x, string y)
{
	for (int i = 0; i != x.size(); i++)
	{
		for (int j = 0; j != y.size(); j++)
		{
			if (x[i] == y[j])
			{
				LCS[i + 1][j + 1] = LCS[i][j] + 1;
			}
			else
			{
				LCS[i + 1][j + 1] = max(LCS[i][j + 1], LCS[i + 1][j]);
			}
		}
	}

	cout << LCS[x.size()][y.size()] << endl;
}

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

	for (int i = 0; i < 1000; i++)
	{
		LCS[0][i] = 0;
		LCS[i][0] = 0;
	}

	for (int i = 0; i < q; i++)
	{
		string x, y;
		cin >> x >> y;
		solve(x, y);
	}
	return 0;
}

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{
		return L(i,j-1)>L(i-1,j)?L(i,j-1):L(i-1,j);
	}
}

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

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

static const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for ( int i = 1; i <= n; i++ ) {
    cin >> p[i - 1] >> p[i];
  }

  for ( int i = 1; i <= n; i++ ) m[i][i] = 0;

  for ( int l = 2; l <= n; l++ ) {
    for ( int i = 1; i <= n - l + 1; i++ ) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for ( int k = i; k <= j - 1; k++ ) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;

  return 0;
}

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

using namespace std;

int main() {
	int q;
	string strX, strY;

	cin >> q;
	for (int n = 0; n < q; n++) {
		cin >> strX;
        cin >> strY;
		char X[strX.length() + 1];
		for (int x = 0; x < strX.length(); x++) {//文字列を配列に入れる
			X[x + 1] = strX[x];
		}

        char Y[strY.length() + 1];
		for (int y = 0; y < strY.length(); y++) {//文字列を配列に入れる
			Y[y + 1] = strY[y];
		}

        int c[strX.length()+1][strY.length()];
        int maxLen = 0;

		for (int i = 0; i<=strX.length(); i++){
            for(int j = 0; j<=strY.length(); j++){
                if(i == 0 || j == 0){
                    c[i][j] = 0;
                }
                else 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]);
                }

                if(c[i][j] > maxLen){
                    maxLen = c[i][j];
                }
            }
        }
        cout << maxLen << endl;
	}

    return 0;
}