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<cstdio>
#define N 1000
using namespace std;

int ans[N+1][N+1];

int deal(string s1,string s2){
    int maxl=-1;

    for(int i=0;i<s1.length();i++){
        ans[i][0]=0;
    }
    for(int j=0;j<s2.length();j++){
        ans[0][j]=0;
    }
    for(int i=1;i<s1.length();i++){
        for(int j=1;j<s2.length();j++){
            if(s1[i]==s2[j]){
                ans[i][j]=ans[i-1][j-1]+1;
            }else{
                ans[i][j]=max(ans[i-1][j],ans[i][j-1]);
            }
            maxl=max(maxl,ans[i][j]);
        }
    }
    return maxl;
}

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

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

#define MAX 1000

int lcs(string x, string y) {
  int c[MAX + 1][MAX + 1];
  int m = x.size();
  int n = y.size();
  int maxl = 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] = max(c[i - 1][j], c[i][j - 1]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }

  return maxl;
}

int main() {
  string s1, s2;
  int q;
  cin >> q;

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

  return 0;
}

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

#define ll long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

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

int q;
string x, y;
int dp[1111][1111];

void solve() {
   for (int i = 0; i < x.size(); i++) {
      for (int j = 0; j < y.size(); 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]);
         }
      }
   }
   cout << dp[x.size()][y.size()] << endl;
}

int main() {
   cin >> q;
   while (q--) {
      cin >> x >> y;
      solve();
   }

   return 0;
}

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 maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;

    for (int i = 1; i <= X.size(); i++) {
        for (int j = 1; j <= Y.size(); 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<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define INFTY 2000000001
#define MAX 1001

char str[150][2][MAX];
int n = 0;
int c[MAX][MAX];

void lcs(int i) {
  int len0 = strlen(str[i][0]);
  int len1 = strlen(str[i][1]);
  for (int j = 0; j < len0; j++) c[j][0] = 0;
  for (int j = 0; j < len1; j++) c[0][j] = 0;
  for (int m = 0; m < len0; m++) {
    for (int n = 0; n < len1; n++) {
      if (str[i][0][m] == str[i][1][n]) {
        c[m+1][n+1] = c[m][n] + 1;
      } else if (c[m][n-1] >= c[m-1][n]) {
        c[m+1][n+1] = c[m+1][n];
      } else {
        c[m+1][n+1] = c[m][n+1];
      }
    }
  }
}

int main() {
  scanf("%d", &n);
  for ( int i = 0; i < n; i++ ) {
    scanf("%s", str[i][0]);
    scanf("%s", str[i][1]);
  }

  for (int i = 0; i < n; i++) {
    lcs(i);
    printf("%d\n", c[strlen(str[i][0])][strlen(str[i][1])]);
  }

  return 0;
}

C++: #include<iostream>
#include<string>
int main()
{
  int n;
  int an[1000];
  int l=0;
  std::cin>>n;
  std::string x;
  std::string y;
  int ans[1000][1000];
  int j,k;
  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<1000;j++){
      for(int k=0;k<1000;k++){
	ans[j][k]=0;
      }
    }
    for(int j=0;j<x.size();j++){
      for(int k=0;k<y.size();k++){
	if(x[j]==y[k]){
	  ans[j][k]=ans[j-1][k-1]+1;
	}else{
	  if(ans[j][k-1]>ans[j-1][k]){
	    ans[j][k]=ans[j][k-1];
	  }else{
	    ans[j][k]=ans[j-1][k];
	  }
	}
      }
    }
    std::cout<<ans[x.size()-1][y.size()-1]<<'\n';  
  }
  return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

char a[1005],b[1005];
int n,c[1005][1005];

int lcs(){
    int n=strlen(a);
    int m=strlen(b);
    for(int i=0;i<n;i++) c[i][0]=0;
    for(int j=0;j<m;j++) c[0][j]=0;
    int ans=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(a[i]==b[j])
                c[i][j]=c[i-1][j-1]+1;
            else
                c[i][j]=max(c[i-1][j],c[i][j-1]);
            ans=max(c[i][j],ans);
        }
    }
    return ans;
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        scanf("%s%s",a,b);
        printf("%d\n",lcs());
    }
    return 0;
}