Go: // AOJ ALDS1_10_C: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C

package main

import (
	"fmt"
)

func main() {
	var dataNum int
	var s1, s2 string

	fmt.Scanf("%d", &dataNum)

	for i := 0; i < dataNum; i++ {
		fmt.Scanf("%s", &s1)
		fmt.Scanf("%s", &s2)

		lcs := make([][]int, len(s1)+1)
		for j := 0; j < len(s1)+1; j++ {
			lcs[j] = make([]int, len(s2)+1)
		}

		for m, vs1 := range s1 {
			for n, vs2 := range s2 {
				if vs1 == vs2 {
					lcs[m+1][n+1] = lcs[m][n] + 1
				} else if lcs[m+1][n] > lcs[m][n+1] {
					lcs[m+1][n+1] = lcs[m+1][n]
				} else {
					lcs[m+1][n+1] = lcs[m][n+1]
				}
			}
		}

		// Answer
		fmt.Println(lcs[len(s1)][len(s2)])
	}
}

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

#define MAX_N 1000
#define MAX_M 1000

int n, m;
std::string s, t;

int dp[MAX_N+1][MAX_M+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] = std::max(dp[i][j+1], dp[i+1][j]);
      }
    }
  }
}

int main() {
  int q;
  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    std::cin >> s >> t;
    n = s.size();
    m = t.size();
    solve();
    std::cout << dp[n][m] << std::endl;
  }
}

C++: #include<iostream>
#include<vector>
#include<string>
using namespace std;
string s1,s2;
int solve(int l1,int l2){
  //  cout << l1 << ' ' << l2 << endl;
  if(l1 <= 0 or l2 <= 0)return 0;
  if(s1[l1] == s2[l2])return solve(l1-1,l2-1) + 1;
  return max(solve(l1,l2-1), solve(l1-1,l2));
}

int main(){
  int n;
  cin >> n;
  cin.ignore();
  for(int i = 0;i < n;i++){
    getline(cin ,s1);
    getline(cin ,s2);
    cout << solve(s1.size(),s2.size()) << endl;
  }
}

C++: #include<stdio.h>
#include<string.h>

#define N 1000
#define S 150

int LCSLength(char *, char *);

int main(){
  int q, i;
  char X[N];
  char Y[N];
  int LCS[S];
  scanf("%d", &q);
  for(i=0; i<q; i++){
    scanf("%s", X);
    scanf("%s", Y);
    LCS[i]=LCSLength(X, Y);
  }

  for(i=0; i<q; i++){
    printf("%d\n", LCS[i]);
  }

  return 0;
}//main

int LCSLength(char *X, char *Y){
  int m, n, i, j;
  int c[N+1][N+1];
  m = strlen(X);
  n = strlen(Y);
  for(i=1; i<=m; i++){
    c[i][0]= 0;
  }
  for(j=0; j<=n; j++){
    c[0][j]= 0;
  }

  for(i=1; i<=m; i++){
   for(j=1; j<=n; j++){
     if(X[i-1]==Y[j-1]) c[i][j]=c[i-1][j-1]+1;
     else if(c[i-1][j]>=c[i][j-1]) c[i][j]=c[i-1][j];
     else c[i][j]=c[i][j-1];
    }
   }
   return c[m][n];
  }

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int q,i,j,k,l1,l2,m,n;
    string X,Y;
    cin>>q;
    for(i=0; i<q; i++)
    {
        cin>>X>>Y;
        l1=X.length()+1;
        l2=Y.length()+1;
        int arr[l2][l1];
        for(k=0; k<l1; k++)
        {
            arr[0][k]=0;
        }
        for(k=0; k<l2; k++)
        {
            arr[k][0]=0;
        }
        for(k=1; k<l2; k++)
        {
            for(j=1; j<l1; j++)
            {
                if(Y[k-1]==X[j-1])
                {
                    arr[k][j]=1+arr[k-1][j-1];
                }
                else
                {
                    m=arr[k-1][j];
                    n=arr[k][j-1];
                    arr[k][j]=max(m,n);
                }
            }
        }
        cout<<arr[l2-1][l1-1]<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define repp(i, m, n) for(int i = m; i < n; i++)
const int    INF = 100000000;
const double EPS = 1e-10;
const int    MOD = 1000000007;
using namespace std;
typedef long long ll;
typedef pair<int, int> pai;
typedef pair<ll,ll> pal;
int q;
string x, y;
int n, m;
int dp[1001][1001];
int main()
{
	cin >> q;
	for(int i=0; i<q; i++)
	{
		for(int j=0; j<1001; j++)
		{
			for(int k=0; k<1001; k++)
			{
				dp[j][k]=0;
			}
		}
		cin >> x >> y;
		n=x.size();
		m=y.size();
		for(int j=1; j<=n; j++)
		{
			for(int k=1; k<=m; k++)
			{
				if(x[j-1]==y[k-1])
				{
					dp[j][k]=dp[j-1][k-1]+1;
				}
				else
				{
					dp[j][k]=max(dp[j-1][k], dp[j][k-1]);
				}
			}
		}
		cout << dp[n][m] << endl;
	}
	return 0;
}

C++: from string import ascii_letters

def llcs(x, y):
    pm = dict((zip(ascii_letters, [0] * 52)))
    for c in pm:
        for i, xc in enumerate(x):
            if c == xc:
                pm[c] |= (1 << i)
    
    V = (1 << len(x)) - 1
    
    for yc in y:
        V = ((V + (V & pm[yc])) | (V & ~pm[yc]))
    
    ans = bin(V)[-len(x):].count('0')
    
    return ans

from sys import stdin

def solve():
    file_input = stdin
    q = int(file_input.readline())
    for i in range(q):
        s1 = file_input.readline().rstrip()
        s2 = file_input.readline().rstrip()
        print(llcs(s1, s2))

solve()

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

signed main() {
  int n;
  cin >> n;
  for (int c = 0; c < n; c++) {
    string x, y;
    cin >> x >> y;

    int dp[x.size() + 1][y.size() + 1];
    for (int i = 0; i < x.size() + 1; i++) {
      dp[i][0] = 0;
    }

    for (int j = 0; j < y.size() + 1; j++) {
      dp[0][j] = 0;
    }
      
    for (int i = 1; i < x.size() + 1; i++) {
      for (int j = 1; j < y.size() + 1; j++) {
	if (x[i - 1] == y[j - 1]) {
	  dp[i][j] = dp[i - 1][j - 1] + 1;
	} else {
	  dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
	}
      }
    }

    cout << dp[x.size()][y.size()] << endl;
  }

  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;int l(string a,string b){int m=a.size(),n=b.size(),i=0,j,s=0,c[1001][1001]={};for(a=' '+a,b=' '+b;++i<=m;)for(j=0;++j<=n;s=s<c[i][j]?c[i][j]:s)c[i][j]=a[i]==b[j]?c[i-1][j-1]+1:c[i-1][j]>c[i][j-1]?c[i-1][j]:c[i][j-1];return s;}main(){string a,b;int n;for(cin>>n;n--;cin>>a>>b,cout<<l(a,b)<<endl);}

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

using namespace std;

int N=1000;

int LCS(string x,string y){
	int c[N+1][N+1],LCSlen=0;
	int m=x.size();
	int n=y.size();
	x=' '+x;
	y=' '+y;
	for(int i=1;i<=m;i++) c[i][0]=0;
	for(int j=1;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]=c[i-1][j]>c[i][j-1] ? c[i-1][j]:c[i][j-1];
			LCSlen=LCSlen>c[i][j] ? LCSlen:c[i][j];
		}
	}
	return LCSlen;
}

int main(){
	string x,y;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		cin>>x;
		cin>>y;
		printf("%d\n",LCS(x,y));
	}
}

C++: import java.util.*;

public class alds1_10 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String X = null, Y = null;
        int[][] dp = new int[1001][1001];

        int q = sc.nextInt();

        for (int i = 0; i < q; ++i) {
            X = sc.next();
            Y = sc.next();
            for (int x = 0; x < X.length(); ++x) {
                for (int y = 0; y < Y.length(); ++y) {
                    if (X.charAt(x) == Y.charAt(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];
                        }
                    }
                }
            }
            System.out.printf("%d\n", dp[X.length()][Y.length()]);
        }
    }
}