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>
using namespace std;

int main()
{
    int N;
    cin >> N;
    for(int k=0;k<N;k++)
    {
        string word1,word2;
        cin >> word1;
        cin >> word2;
        int L[2000][2000];
        for(int i=0;i<word1.size();i++)
           for(int j=0;j<word2.size();j++)
           {
               if(i<0 || j<0 )
                 L[i][j]=0;
               else if(word1[i]==word2[j])
                 L[i][j]=1+L[i-1][j-1];
               else
                 L[i][j]=max(L[i-1][j],L[i][j-1]);
           }
        cout << L[word1.size()-1][word2.size()-1] << endl;
    }
}

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

int lcs(string s1, string s2){
    int n=s1.size();
    int m=s2.size();

    s1=' '+s1;
    s2=' '+s2;

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

    for(int i=1; i<=n; i++){
        for(int j=1; j<=m; 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]);
        }
    }

    return dp[n][m];

}

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

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

int dp[1001][1001] = {{0}};

int LCS(const string & X, const string & Y){
    auto len_x = X.length();
    auto len_y = Y.length();
    for (int i = 0; i != len_x; ++i){
        for (int j = 0; j != len_y; ++j){
            if (X[i] == Y[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]);
            }
        }
    }
    return dp[len_x][len_y];
}

int main()
{
    ios::sync_with_stdio(false);
    
    int q = 0;
    cin >> q;
    
    string X;
    string Y;
    while (q--){
        cin >> X >> Y;
        cout << LCS(X, Y) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
using namespace std;

bool findSubStr(string& shortword, vector<vector<int> >& charMap) {
	int i,j;
	j=0;
	for(i=0; i<(int)shortword.size(); ++i) {
		if(charMap[shortword[i]-'a'].empty()) return false;
		auto itr=charMap[shortword[i]-'a'].begin();
		while(itr!=charMap[shortword[i]-'a'].end()) {
			if(*itr<j) {
				++itr;
			} else {
				break;
			}
		}
		if(itr==charMap[shortword[i]-'a'].end()) return false;
		j=*itr;
	}
	return true;
}

int longestSQ(string& shortword, string& longword) {
	int i,j;
	string tempstr,prestr;
	vector<vector<int> > charMap(26);
	queue<string> targetword;
	map<string,bool> inqueue;
	for(i=0; i<(int)longword.size(); ++i) {
		charMap[longword[i]-'a'].push_back(i);
	}
	targetword.push(shortword);
	while(!targetword.empty()) {
		tempstr=targetword.front();
		if(findSubStr(tempstr,charMap)) return (int)tempstr.size();
		j=(int)tempstr.size();
		for(i=0; i<j; ++i) {
				tempstr=targetword.front();
				if(i==0 || tempstr[i]!=tempstr[i-1]) {
					tempstr.erase(tempstr.begin()+i);
					if(inqueue.find(tempstr)==inqueue.end()) {
						targetword.push(tempstr);
						inqueue[tempstr]=true;
					}
				}
		}
		targetword.pop();
	}
	return 0;
}

int main() {
	int i,q;
	cin >> q;
	string wordA,wordB;
	for(i=0; i<q; ++i) {
		cin >> wordA >> wordB;
		if(wordA.size()<wordB.size()) {
			printf("%d\n",longestSQ(wordA,wordB));
		} else {
			printf("%d\n",longestSQ(wordB,wordA));
		}
	}
	return 0;
}

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

int memo[1001][1001];
string s,t;

int dfs(int i, int j)
{
    if(i == 0 || j == 0) return 0;
    
    if(memo[i][j] != -1)return memo[i][j];
    
    int res = 0;
    
    if(s[i-1] == t[j-1]) res = dfs(i-1, j-1) + 1;
    else {
        res = max(dfs(i - 1, j), dfs(i, j - 1));
    }
    return memo[i][j] = res;
}

int main()
{
    int n;
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> s >> t;
        fill((int*)memo, (int*)(memo + (int)max(s.size(), t.size()) + 1), -1);
        cout << dfs(s.size(), t.size()) << endl;
    }
}

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

using namespace std;

int lic(string X,string Y) {
	int c[1001][1001];
	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 = 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]==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 X, Y;
	int n;
	cin >> n;
	for (int i = 0;i < n;i++) {
		cin >> X >> Y;
		cout << lic(X, Y)<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxx = 1010;
int c[maxx][maxx];

int lcs(char s1[],char s2[]) {
	int m = strlen(s1), n = strlen(s2);
	for (int i = 1; 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 (s1[i] == s2[j])
				c[i][j] = c[i - 1][j - 1] + 1;
			else
				c[i][j] = max(c[i - 1][j], c[i][j - 1]);
		}
	}
	return c[m][n];
}
int main()
{
	char s1[maxx], s2[maxx];
	int t;
	cin >> t;
	getchar();
	for (int i = 0; i < t; i++) {
		cin.getline(s1,1010);
		cin.getline(s2,1010);
		printf("%d\n", lcs(s1, s2));
	}

	return 0;
}