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<cstring>
#include<algorithm>
#define MAX_N 1000

int solve(std::string s,std::string t){
        int dp[MAX_N][MAX_N];
        memset(dp,0,sizeof(dp));
        int n,m;
        n=s.size(),m=t.size();
        for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        if(s[i]==t[j])
                                dp[i+1][j+1]=dp[i][j]+1;
                        else
                                dp[i+1][j+1]=std::max(dp[i][j+1],dp[i+1][j\
]);
                }
        }
        return dp[n][m];
}

int main(){
        int q;
        std::cin>>q;
        for(int i=0;i<q;i++){
                std::string s,t;
                std::cin >> s>>t;
                std::cout << solve(s,t) << std::endl;
        }
        return 0;
}

C++: #if 0

#endif

#include <iostream>
#include <string>
using namespace std;

int main(){
	int q;
	string X,Y;
	string x,y;
	int L[1000][1000];
	int MAX;
	for(int i=0;i<1000;++i) //???????????\????????????L????????????
		for(int j=0;j<1000;++j)
			L[i][j]=0;
	
	cin >> q;
	for(int i=0;i<q;++i){
		cin >> X >> Y;
		for(int j=1;j<X.length()+1;++j)
			for(int k=1;k<Y.length()+1;++k){
				if(X[j-1]==Y[k-1])
					L[j][k]=1+L[j-1][k-1];
				else
					L[j][k]=max(L[j-1][k],L[j][k-1]);
				MAX = max(MAX,L[j][k]);
			}
	cout << MAX << endl;
	}
}

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

int main()
{
  int q;scanf("%d",&q);
  for(int Q=0;Q<q;Q++)
  {
  string X,Y;cin>>X>>Y;
  vector<vector<int>> dp(X.size()+1,vector<int>(Y.size()+1,0));
  for(int i=0;i<(int)X.size();i++)for(int j=0;j<(int)Y.size();j++)
  {
    if(X[i]==Y[j])dp[i+1][j+1]=dp[i][j]+1;//max({dp[i][j]+1,dp[i][j+1],dp[i+1][j]});
    else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
  }
  printf("%d\n",dp[X.size()][Y.size()]);
  }
}

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

int LCS(string p, string q) {
	vector<vector<int> > f(p.size()+1, vector<int>(q.size()+1,0));
	int t;
	for (int i = 1; i <= p.size(); i++) {
		for (int j = 1; j <= q.size(); j++) {
			if (p[i-1] == q[j-1])
				f[i][j] = f[i - 1][j - 1] + 1;
			else if (f[i][j - 1] > f[i - 1][j])
				f[i][j] = f[i][j - 1];
			else
				f[i][j] = f[i - 1][j];
		}
	}
	return f[p.size()][q.size()];
}

int main() {
	int n;
	string p[300];
	cin >> n;
	for (int i = 0; i < n; i ++) {
		cin >> p[2*i] >> p[2*i + 1];
	}
	for (int i = 0; i < n; i++) {
		cout << LCS(p[2*i], p[2 * i + 1])<<endl;
	}
	return 0;
}

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

#define rep(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define all(x) (x).begin(), (x).end()
using i32 = int32_t;
using i64 = int64_t;
using f32 = float;
using f64 = double;
using P   = pair<int, int>;

template <class T>
bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}
template <class T>
bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template <class T>
void dump_vec(const vector<T>& v) {
    auto len = v.size();
    rep(i, 0, len) {
        cout << v[i] << (i == (int)len - 1 ? "\n" : " ");
    }
}

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} FASTIO;

//---------------------------------------------------------------------------------------------------

/// 最小共通部分列: longest common subsequence
/// O(|S||T|)
int lcs(const string& s, const string& t) {
    int slen = s.size(), tlen = t.size();
    vector<vector<int>> dp(slen + 5, vector<int>(tlen + 5, 0));
    rep(i, 0, slen) rep(j, 0, tlen) {
        if (s[i] == t[j]) {
            chmax(dp[i + 1][j + 1], dp[i][j] + 1);
        }
        chmax(dp[i + 1][j + 1], dp[i + 1][j]);
        chmax(dp[i + 1][j + 1], dp[i][j + 1]);
    }

    return dp[slen][tlen];
}
//---------------------------------------------------------------------------------------------------

signed main() {
    int n;
    cin >> n;
    rep(i, 0, n) {
        string s, t;
        cin >> s >> t;
        cout << lcs(s, t) << "\n";
    }
}

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 <algorithm>
#define MAX_N 1001
using namespace std;
//lcs
int n,m;
string s,t;
int dp[MAX_N+1][MAX_N+1];
void solve(){
  for(int i=0;i<n;i++)
    for(int j=0;j<m;j++){
      if(s[i]==t[j]) dp[i+1][j+1]=dp[i][j]+1;
      else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
    }
  cout<<dp[n][m]<<endl;
}
 
 
int main(){
  int N;
  cin>>N;
  while(N--){
    cin>>s>>t;
    n=s.size();
    m=t.size();
    solve();
  }
  return 0;
}