Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = dp[i-1][j-1]+1
				} else if S[i-1] != T[j-1] && dp[i-1][j] > dp[i][j-1] {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i][j-1]
				}
				// if S[i-1] == T[j-1] {
				// 	dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				// } else {
				// 	dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				// }
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

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

using namespace std;

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

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

vector<vector<int> > DP(1000,vector<int>(1000));

void init(int n,int m){
    rep(i,n){
        rep(j,m){DP[i][j]=-1;}
    }
}

int LCS(char a[],char b[],int n,int m){
    if(n<0||m<0)return 0;
    if(DP[n][m]!=-1)return DP[n][m];
    if(a[n]==b[m])return DP[n][m]=1+LCS(a,b,n-1,m-1);
    return DP[n][m]=max(LCS(a,b,n-1,m),LCS(a,b,n,m-1));
}

int main(){
    int q;cin>>q;
    char s[1001],t[1001];
    rep(i,q){
        cin>>s>>t;
        int n=strlen(s),m=strlen(t);
        init(n,m);
        int ans=LCS(s,t,n-1,m-1);
        printf("%d\n",ans);
    }
}

C++: #include<iostream>
#include<math.h>
#include<stdlib.h>
#include<string>
#include<limits.h>
#include<utility>
#include<vector>
#include<tuple>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i,n) for(int i=0;i<n;i++)
#define ALL(A) A.begin(),A.end()


int main() {
	int n;
	cin >> n;
	rep(i, n) {
		string x, y;
		cin >> x >> y;
		int xl = x.length(), yl = y.length();
		vector<vector<int>> com(xl, vector<int>(yl, 0));
		rep(j, xl) {
			rep(k, yl) {
				if (j == 0) {
					if (k == 0) {
						if (x[j] != y[k]) {
							com[j][k] = 0;
						}
						else {
							com[j][k] = 1;
						}
					}
					else {
						if (com[j][k-1] == 1) {
							com[j][k] = 1;
						}
						else {
							if (x[j] != y[k]) {
								com[j][k] = 0;
							}
							else {
								com[j][k] = 1;
							}
						}
					}
				}
				else {
					if (k == 0) {
						if (com[j-1][k] == 1) {
							com[j][k] = 1;
						}
						else {
							if (x[j] == y[k]) {
								com[j][k] = 1;
							}
							else {
								com[j][k] = 0;
							}
						}
					}
					else {
						if (x[j] == y[k]) {
							com[j][k] = com[j - 1][k - 1]+1;
						}
						else {
							com[j][k] = max(com[j][k - 1], com[j - 1][k]);

						}
					}
				}
				//cout << com[j][k] <<" ";
			}
			//cout << x[j]<<endl;
		}
		cout << com[xl - 1][yl - 1] << endl;
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }


int main() {
	
	int q;
	cin >> q;
	for(int cnt=0; cnt<q; cnt++){
		string s,t;
		cin >> s >> t;
		vector<vector<int>> dp(s.size()+1,vector<int>(t.size()+1));
		for(int i=0; i<s.size(); i++){
			for(int j=0; j<t.size(); j++){
				if(s.at(i)==t.at(j)) dp.at(i+1).at(j+1) = dp.at(i).at(j) + 1;
				else  dp.at(i+1).at(j+1) = max( dp.at(i).at(j+1) , dp.at(i+1).at(j) );
			}
		}
		cout << dp.at(s.size()).at(t.size())<<endl;
	}
}

C++: //ALDS1_10_C:Longest Common Subsequence
#include <bits/stdc++.h>
using namespace std;
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807

int main(){
    int q;
    cin >> q;
    for(int i=0; i<q; i++){
        string X,Y;
        cin >> X >> Y;
        int lcs[1001][1001];
        for(int i=0; i<=X.size(); i++){
            for(int j=0; j<=Y.size(); j++){
                if(i == 0 || j == 0) lcs[i][j] = 0;
                else if(X[i-1] == Y[j-1]) lcs[i][j] = lcs[i-1][j-1] + 1;
                else lcs[i][j] = max(lcs[i][j-1], lcs[i-1][j]);
            }
        }
        cout << lcs[X.size()][Y.size()] << endl;
    }
}

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

int main(void){
  string st1, st2;
  int q, val[151][151];

  cin >> q;

  for(int i=0; i<q; i++){
    cin>>st1;
    cin>>st2;

    for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){

	if(j==0 || k==0) val[j][k] = 0;
	else{

	  if(st1[j-1] == st2[k-1]){
	    val[j][k] = max(val[j-1][k], val[j][k-1]+1);
	  }else{
	    val[j][k] = max(val[j-1][k], val[j][k-1]);
	  }
	}

	if(val[j][k] >= st1.length() || val[j][k] >= st1.length()){
	  val[j][k] = min(st1.length(), st2.length());
	}
      }
    }

    /*
      for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){
      cout << val[j][k] << " ";
      }
      cout << "\n";
      }
    */

    cout << val[st1.length()][st2.length()] << "\n";
  }

  return 0;
}

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

int lcs(string X, string Y) {
	int dp[1000 + 1][1000 + 1];
	int max = 0;
	X = ' ' + X;
	Y = ' ' + Y;
	for (unsigned int i = 1; i <= X.size() - 1; i++)
		dp[i][0] = 0;

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

			max = (max < dp[i][j]) ? dp[i][j] : max;
		}
	}

	return max;
}

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

	for (int i = 0; i < N; i++) {
		string X, Y;
		cin >> X >> Y;

		cout << lcs(X, Y) << endl;
	}

	return 0;
}