Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

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; // X[0] に空白を挿入
    Y = ' ' + Y; // Y[0] に空白を挿入
     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;
                                                }

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

int lcs(string str1, string str2) {
	int n1 = str1.size();
	int n2 = str2.size();
	int A[n1 + 1][n2 + 1];
	for (int i = 0; i <= n1; i++) A[i][0] = 0;
	for (int j = 1; j <= n1; j++) A[0][j] = 0;

	for (int i = 1; i <= n1; i++) {
		for (int j = 1; j <= n2; j++) { 
			if (str1[i - 1] == str2[j - 1]) {
				A[i][j] = A[i - 1][j - 1] + 1; 
			} else {
				A[i][j] = max(A[i - 1][j], A[i][j - 1]);
			}
		}
	}
	return A[n1][n2];
}

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>
#include <algorithm>
using namespace std;
  
static const int N = 1000;
  
int las(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;
}

C++: #include "stdafx.h"
#include<iostream>
#include<algorithm>
#include<string>
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++) {
			cout << c[i][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() {
	int n;
	string x, y;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x >> y;

		cout << lcs(x, y) << endl;
	}

	
}

C++: #include <iostream> 
#include <cstring> 
using namespace std;
int c[1001][1001];

int lcs(string x, string y)
{
    int maxl = 0;
    int m = x.length(), n = y.length();
 
    for (int i = 0; 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-1] == y[j-1])
            { 
                // if(i==0 || j==0) c[i][j] = 1;
                c[i][j] = c[i-1][j-1] + 1; 
            }else
            {
                // if(i==0 || j==0) c[i][j] = 0;
                c[i][j] = max(c[i][j-1], c[i-1][j]);
            }
            maxl = max(maxl, c[i][j]);
        } 
    } 
    // printf("%d\n", c[m][n]);
    return c[m][n];
}

int main()
{
    
 //   freopen("n4.txt","r",stdin);
    string x,y;
    int q;
    cin>>q;
    while(q--)
    {
        cin>>x>>y;
        int ans = lcs(x, y);
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define V vector<long long>
#define VV vector<vector<long long>>
#define VVV vector<vector<vector<long long>>>
#define rep(i,n) for(ll (i)=0;(i)<(n);++(i))
using namespace std;

int main() {
    ll q;
    cin>>q;
    rep(i,q){
        string a,b;
        cin>>a>>b;
        VV dp(a.size()+1,V(b.size()+1,0));
        rep(j,a.size()){
            rep(k,b.size()){
                if(a[j]==b[k]){
                    dp[j+1][k+1]=dp[j][k]+1;
                }else{
                    dp[j+1][k+1]=max(dp[j+1][k],max(dp[j][k+1],dp[j][k]));
                }
            }
        }
        cout<<dp[a.size()][b.size()]<<endl;
    }
}

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

int dp[1005][1005];

int lcs(string x, string y){
    for (int i = 1; i <= x.size(); i++){
        for (int j = 1; j <= y.size(); j++)
        {
            if(x[i-1] != y[j-1])
                dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
            else
                dp[i][j] = dp[i-1][j-1] + 1;
        }
    }
    return dp[x.size()][y.size()];
}

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

C++: #include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int MAX=1001;
int lcs(string X,string Y) 
{
	int i=0,j=0,c[MAX][MAX],x=X.length(),y=Y.length();
	for(i=0;i<=x;i++)c[i][0]=0;
	for(i=0;i<=y;i++)c[0][i]=0;
	for(i=1;i<=x;i++) 
	for(j=1;j<=y;j++) 
	if(X[i-1]==Y[j-1])c[i][j]=c[i-1][j-1]+1; 
	else c[i][j]=max(c[i][j-1],c[i-1][j]);
	return c[x][y];
}
int main()
{
	int n;cin>>n;
	string X,Y;
	while(n--)
	{
		cin>>X>>Y;
		cout<<lcs(X,Y)<<endl;
	}
}