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>
#include<algorithm>
using namespace std;
static const int N = 1000;
 
int lcs(string X,string Y){
  int c[N + 1][N + 1];
  int m=X.size();
  int n=Y.size();
  int maxl=0;
  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]=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 <bits/stdc++.h>
       
using namespace std;
typedef long long ll;
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; }

int main() {
  int q;
  cin >> q;

  for (int i = 0; i < (int)(q); ++i) {
    string X, Y;
    cin >> X >> Y;

    int M = X.size(), N = Y.size();
    vector<vector<int>> dp(M + 1, vector<int>(N + 1, 0));
    for (int i = 0; i < (int)(M); ++i) for (int j = 0; j < (int)(N); ++j) {
      if (X[i] == Y[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      chmax(dp[i + 1][j + 1], max(dp[i][j + 1], dp[i + 1][j]));
    }
    cout << dp[M][N] << endl;
  }

  return 0;
}

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

int lcs(string S1,string S2){
	int c[N+1][N+1];
	int S1_size = S1.size();
	int S2_size = S2.size();
	
	for(int i=1;i<S1_size;i++) c[i][0] = 0;
	for(int i=1;i<S2_size;i++) c[0][i] = 0;
	
	S1 = ' '+S1;
	S2 = ' '+S2;
	
	int max_lcs=0;
	for(int j=1;j<=S1_size;j++){
		for(int k=1;k<=S2_size;k++){
			if(S1[j]==S2[k]){
				c[j][k] = c[j-1][k-1] + 1;
			}else{
				c[j][k] = max(c[j-1][k],c[j][k-1]);
			}
			max_lcs = max(max_lcs,c[j][k]);
		}
	}
	return max_lcs;
}

int main() {
	// your code goes here
	int q;
	string str1,str2;
	cin>>q;
	for(int i=0;i<q;i++){
		cin>>str1>>str2;
		cout<<lcs(str1,str2)<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<cstdlib>
#define MAX_N 1000

int solve(std::string s,std::string t){
        int dp[MAX_N][MAX_N]={0};
        int n,m;
        n=s.size();
       m=t.size();
        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]=max(dp[i][j+1],dp[i+1][j]);
                }
        }
        return dp[n][m];
}

int main(){
        int q;
        std::cin>>q;
        for(int i=0;i<q;i++){
                std::string s,t;
                std::cin >> s>>t;
                std::cout<<solve(s,t)<<std::endl;
        }
        return 0;
}

C++: #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1001
void solve(void);
int q,count[MAX][MAX]={},xlength,ylength;
char X[MAX],Y[MAX];

void solve(void){
  int i,j;

  xlength=strlen(X);
  ylength=strlen(Y);
  
  for(i=1;i<=xlength;i++){
    for(j=1;j<=ylength;j++){
      if(X[i-1]==Y[j-1]) count[i][j]=count[i-1][j-1]+1;
      else if(count[i-1][j]>=count[i][j-1]) count[i][j]=count[i-1][j];
      else count[i][j]=count[i][j-1];
    }
  }
}
    
int main()
{
  int i;
  
  scanf("%d",&q);

  for(i=1;i<=q;i++){
    scanf("%s",X);
    scanf("%s",Y);
    solve();
    printf("%d\n",count[xlength][ylength]);
  }

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

  return 0;
}

C++: import sys
sys.setrecursionlimit(10000)

def calc(x,y):
    i = len(x)
    j = len(y)
    if not d[i][j] == None:
        return d[i][j]
    else:
        if x[-1] == y[-1]:
            tmp = calc(x[:-1],y[:-1])+1
            d[i][j] = tmp
            return tmp
        else:
            tmp = max(calc(x[:-1],y),calc(x,y[:-1]))
            d[i][j] = tmp
            return tmp
        
q = int(input())
for i in range(q):
    x = input()
    y = input()
    d = [[0 if i == 0 or j == 0 else None for i in range(len(y)+1)] for j in range(len(x)+1)]
    print(calc(x,y))

C++: #include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
int dp[1001][1001];
int solve(string a,string b)
{	
	
	int maxn=0;
	int len1=a.size(),len2=b.size();
	

	for(int i=1;i<=len1;i++)
		for(int j=1;j<=len2;j++)
		{
			if(a[i-1]==b[j-1])
				dp[i][j]=dp[i-1][j-1]+1;
			else 
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//这个只是表示某个状态的不是最大的，这个是自己设定的
			maxn=max(maxn,dp[i][j]);
		}
		return maxn;
		
		
	
}
int main()
{	int n;
	string a,b;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		cout<<solve(a,b)<<endl;
	}
	return 0;
}

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

int c[10001][10001];

int llcs(string X, string Y)
{
    //cout << "hhh" << endl;
    //cout << X << " " << Y << endl;
    int m=X.size();
    int n=Y.size();
    //cout << m << " " << n << endl;
    int maxl=0;
    X=' '+X;
    Y=' '+Y;

    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++)
        {
            //cout << i << " " << j << c[i][j] << endl;
            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()
{
    int N;
    cin >> N;
    string X,Y;
    for(int i=0;i<N;i++)
    {
        cin >> X;
        cin >> Y;
        cout << llcs(X,Y) << endl;
    }

    return 0;
}

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

string X, Y;
int q, lx, ly;
int dp[1010][1010];

int main() {
    cin >> q;
    
    for (int i=0; i<q; ++i) {
        cin >> X >> Y;
        lx = X.size();
        ly = Y.size();
        for (int i=0; i<=ly; ++i) dp[i][0] = 0;
        for (int i=0; i<=lx; ++i) dp[0][i] = 0;

        for (int j=1; j<=ly; ++j) {
            for (int k=1; k<=lx; ++k) {
                if (Y[j-1]==X[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[ly][lx] << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
void solve(string s, string t){
    int l1=s.size(),l2=t.size();
    vector <vector<int>> dp(l1+10 ,vector <int>(l2+10 ));
    for(int i=0;i<l1;i++){
        for(int j=0;j<l2;j++){
            if(s[i]==t[j]) {
                dp[i+1][j+1] = max(dp[i][j] + 1, dp[i+1][j+1]);
            }else{
                dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]);
            }
        }
    }
    cout<<dp[l1][l2]<<endl;
}
int main(void){
    int n; cin>>n;
    for(int i=0;i<n;i++){
        string s,t; cin>>s>>t;
        solve(s,t);
    }
}