Go: package main

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

func max(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = " " + s1 , " " + s2
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); 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])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

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

int Longest(string x, string y) {
  int L[1001][1001];
  int maxl = -1;
  int xlen = x.size();
  int ylen = y.size();

  x = ' ' + x;
  y = ' ' + y;

  for (int i = 1; i <= xlen; i++) L[i][0] = 0;
  for (int j = 1; j <= ylen; j++) L[0][j] = 0;

  for (int i = 1; i <= xlen; i++) {
    for (int j = 1; j <= ylen; j++) {
      if (x[i] == y[j])
        L[i][j] = L[i-1][j-1] + 1;
      else
        L[i][j] = max(L[i][j-1], L[i-1][j]);

      maxl = max(maxl, L[i][j]);
    }
  }

  return maxl;
}

int main() {
  string X, Y;
  int n;
  int result;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> X;
    cin >> Y;
    result =  Longest(X, Y);
    cout << result << endl;
  }

  


  return 0;
}

C++: #include <iostream> 
#include <cstring> 
using namespace std;
int c[1001][1001];

int lcs(string x, string y)
{
    for (int i = 0; i < x.size(); i++)
    {
        c[i][0] = 0;
    } 
    for (int i = 0; i < y.size(); i++)
    {
        c[0][i] = 0; 
    }
    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][j-1] + 1; 
            }else
            {
                c[i][j] = max(c[i][j-1], c[i-1][j]);
            }
        } 
    } 
    return c[x.size()][y.size()];
}

int main()
{
    string x,y;
    int q;
    cin>>q;
    while(q--)
    {
        cin>>x>>y;
        int ans = lcs(x, y);
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
//BEGIN CUT HERE
struct LongestCommonSubstring{
  struct node{
    int value;
    node *next;
    node(int value,node *next):value(value),next(next){}
  };
  const int INF=1LL<<55LL;
  string x,y;
  LongestCommonSubstring(){}
  LongestCommonSubstring(string x,string y):x(x),y(y){}
  string lcs(){
    int n=x.size(),m=y.size();
    map<char,vector<int> > M;
    for(int j=m-1;j>=0;j--) M[y[j]].push_back(j);
    vector<int> xs(n+1,INF);
    xs[0]=-INF;
    vector<node* > link(n+1);
    for(int i=0;i<n;i++){
      if(M.count(x[i])){
	vector<int> ys=M[x[i]];
	for(int j=0;j<(int)ys.size();j++){
	  int k=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),ys[j]));
	  xs[k]=ys[j];
	  link[k]=new node(y[ys[j]],link[k-1]);
	}
      }
    }
    string res;
    int l=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),INF-1))-1;
    for(node *p=link[l];p!=NULL;p=p->next)
      res.push_back(p->value);
    return res;
  }
};
//END CUT HERE
signed main(){
  int q;
  cin>>q;
  while(q--){
    string x,y;
    cin>>x>>y;
    LongestCommonSubstring lcs(x,y);
    cout<<lcs.lcs().size()<<endl;
  }
  return 0;
}
/*
verified on 2017/06/29
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
*/

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

signed main(){

  int q; cin >> q;
  while(q-- > 0){
    string x,y; cin >> x >> y;
    int dp[x.size()+1][y.size()+1];
    fill(dp[0],dp[x.size()+1],0);
    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+1][j],dp[i][j+1]);
      }
    }
    cout << dp[x.size()][y.size()] << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1e9
#define all(v) v.begin(), v.end()
#define rep(i, n) for(int i = 0; i < n; i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
using namespace std;

int main(){
    int dp[1001][1001];
    string x, y;
    int q, n, xl, yl, ans;
    cin>>q;
    for(int k = 0; k < q; k++){
        cin>>x>>y;
        for(int i = 0; i <= 1000; i++){
            for(int j = 0; j <= 1000; j++){
	            dp[i][j]=0;
            }
            xl = x.size();
            yl = y.size();
            ans = 0;
        }
        for(int i = 1; i <= xl; i++){
            for(int j = 1; j <= yl; 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[xl][yl]<<endl;
    }
  return 0;
}

C++: #include<iostream>

using namespace std;

int dp[1001][1001];

int main(){
    int q, m, n;
    cin >> q;

    while(q-- > 0){
        string x, y;
        cin >> x >> y;
        x = ' ' + x;
        y = ' ' + y;
        m = x.length();
        n = y.length();
        for(int i = 1; i <= m; i++) dp[i][0];
        for(int i = 1; i <= n; i++) dp[0][i];

        int res = 0;
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(x[i] == y[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[m][n]-1 << endl;
    }

    return 0;
}

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

using namespace std;

const int maxn=1010;
int c[maxn][maxn];
string s1,s2;

int lcs(int x,int y)
{
	for(int i=1;i<=x;i++)c[i][0]=0;
	for(int j=1;j<=y;j++)c[0][j]=0;
	s1=' '+s1;
	s2=' '+s2;//插入空格
	int maxl=0; 
	for(int i=1;i<=x;++i){
		for(int j=1;j<=y;++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]);
			}
			maxl=max(maxl,c[i][j]);//每次取最大值并更新 
		}
	}
	return maxl;
}

int main()
{
	int t;
	cin>>t;
	while(t--){
		memset(c,0,sizeof(c));
		cin>>s1>>s2;
		int len1=s1.length();
		int len2=s2.length();
		int ans=lcs(len1,len2);
		cout<<ans<<endl;
	}
	return 0;	
}

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

void chmax(int &a, int b) {
  if (a < b) a = b;
}

int main() {

  int q;
  cin >> q;
  while (q--) {
    string s, t;
    cin >> s >> t;
    int n = s.size(), m = t.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
    rep(i, n) rep(j, m) {
      chmax(dp[i + 1][j + 1], dp[i + 1][j]);
      chmax(dp[i + 1][j + 1], dp[i][j + 1]);
      if (s[i] == t[j]) { chmax(dp[i + 1][j + 1], dp[i][j] + 1); }
    }
    cout << dp[n][m] << endl;
  }

  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;
}