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++: // ALDS1_10_C.cpp
// Dynamic Programming - Longest Common Subsequence

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

int LCS(string s, string t)
{
    int res = 0;
    if (s.empty() || t.empty())
        return res;
    int s_len = s.size();
    int t_len = t.size();
    int len = min(s_len, t_len);
//    int lcs[s_len][t_len];
    vector<vector<int>> lcs(s_len, vector<int>(t_len));
    bool found = false;
    for (int i = 0; i < s_len; i++) {
        if (s[i] == t[0] || found) {
            lcs[i][0] = 1;
            found = true;
        }
    }
    found = false;
    for (int j = 0; j < t_len; j++) {
        if (s[0] == t[j] || found) {
            lcs[0][j] = 1;
            found = true;
        }
    }
    for (int i = 1; i < s_len; i++) {
        for (int j = 1; j < t_len; j++) {
            if (s[i] == t[j]) {
                lcs[i][j] = 1 + lcs[i-1][j-1];
            }
            else {
                lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1]);
            }
        }
    }
    return lcs[s_len - 1][t_len - 1];
}

int main()
{
    int n;
    cin >> n;
    string s, t;
    while (n--) {
        cin >> s >> t;
        int res = LCS(s, t);
        cout << res << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        string X,Y;
        cin >> X >> Y;
        int L[X.length()+10][Y.length()+10];
        for(int j = 0;j < X.length()+10;j++){
            for(int k = 0;k < X.length()+10;k++){
                L[j][k] = 0;
            }
        }
        for(int j = 1;j<=X.length();j++){
            for(int k = 1;k<=Y.length();k++){
                if(Y[k] == X[j]){
                    L[j][k] = 1+L[j-1][k-1];
                }
                else{
                    L[j][k] = max(L[j-1][k],L[j][k-1]);
                }
            }
        }
        cout << L[X.length()][Y.length()] << endl;
    }
return 0;
}

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

int main(){
  int q, cnt, max=0;
  string str1, str2;
  char ch1, ch2;
  cin >> q;
  for(int i=0;i<q;i++){
    cin >> str1 >> str2;
    //cout << "cin >> str1 >> str2" << endl;
    for(int j=0;j<(int)str1.size();j++){
      for(int k=0;k<(int)str2.size();k++){
	//cout << "j = " << j << ", k = " << k << endl;
        cnt = 0;
	int l = 0;
	while(1){
	      //str1[j+l] || str2[k+l] || str1[j+l] == str2[k+l]){
	  ch1 = str1[j+l];
	  //  cout << ch1 << " ";
	  ch2 = str2[k+l];
	  //  cout << ch2 <<endl;
	  if(ch1 == ch2 && ch1 != '\0'){
     	    cnt++;
	    l++;
	    //	    cout << "一致" << endl;
	  }else break;
	}
	if(cnt > max) max = cnt;
      }
    }
    cout << max << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#define MAX 1000


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

int main() {
    std::string s1, s2;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        std::cin >> s1 >> s2;
        
        printf("%d\n", lcs(s1, s2));
    }
    
    return 0;
}

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

int get_lcs_length(std::string X, std::string Y) {
    int m = X.length();
    int n = Y.length();
    vector<vector<int>> c(m + 1, vector<int>(n + 1, 0));
    X = " " + X;
    Y = " " + Y;

    int max_length = 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] = std::max(c[i - 1][j], c[i][j - 1]);
            }
            max_length = std::max(max_length, c[i][j]);
        }
    }
    return max_length;
}

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

}

C++: #include<bits/stdc++.h>

using namespace std;

const int N=1010;

int dp[N][N];

int main()
{
    int n;

    char s1[N],s2[N];

    cin>>n;

    while(n--)
    {
        cin>>(s1+1)>>(s2+1);

        int len1=strlen(s1+1),len2=strlen(s2+1);
        for(int i=1;i<=len1;i++)
        {
            for(int j=1;j<=len2;j++)
            {
                if(s1[i]==s2[j])
                {
                    dp[i][j]=dp[i-1][j-1]+1;
                }else
                {
                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }

        cout<<dp[len1][len2]<<endl;
    }

    return 0;
}