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 <string>
#include <algorithm>
using namespace std;
int dp[1002][1002];

int main(){
	int n;cin >> n;
	string x, y;
	while(n--){
		fill((int*)dp, (int*)(dp+1002), 0);
		cin >> x >> y;
		for(int i = 1;i <= x.size();i++){
			for(int j = 1;j <= y.size();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++: // 17D8104010B 宮本拓輝 10_c c++

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main(){
  int score[1001][1001];
  int n;
  cin>>n;
  int result[n];
  for(int s=0;s<n;s++){
    string x,y;
    cin>>x;
    cin>>y;

    for(int i=0;i<x.size();i++){
      for(int j=0;j<y.size();j++){
        if(x[i]==y[j]){
          score[i+1][j+1]=score[i][j]+1;
        }
        else{
          score[i+1][j+1]=max(score[i+1][j],score[i][j+1]);
        }
      }
    }
    //cout<<score[x.size()][y.size()]<<"\n";
    result[s]=score[x.size()][y.size()];

  }
  for(int i=0;i<n;i++){
    cout<<result[i]<<"\n";
  }

  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 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 = 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<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 = 0; 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++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;

const char *X, *Y;

int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}
	int max = 0;
	for (int i = x_begin; X[i] != '\0'; i++) {
		for (int j = y_begin; Y[j] != '\0'; j++) {
			if (X[i] == Y[j]) {
				int v_lcs = lcs(i + 1, j + 1);
				if (v_lcs + 1 > max) {
					max = v_lcs + 1;
				}
			}
		}
	}
	return table_lcs[x_begin][y_begin] = max;

}

int main() {

	int N;
	cin >> N;
	string stringX, stringY;
	for (int i = 1; i <= N; i++) {
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				table_lcs[i][j] = -1;
			}
		}
		cin >> stringX >> stringY;
		X = stringX.c_str();
		Y = stringY.c_str();
		printf("%d\n", lcs(0, 0));
	}
	return 0;
}

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

using namespace std;
const int N_MAX = 1000;
char x[N_MAX+10], y[N_MAX+10];
int dp[N_MAX][N_MAX];

void solve() {
  int i, j;
  scanf("%s", x);
  scanf("%s", y);
  int N = strlen(x);
  int M = strlen(y);
  //printf("N = %d M = %d", N, M);
  for (i = 0; i < N+1; i++) {
    dp[i][0] = 0;
  }
  for (j = 0; j < M+1; j++) {
    dp[0][j] = 0;
  }
  for (i = 0; i < N; i++) {
    for (j = 0; j < M; 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]);
      }
    }
  }
  printf("%d\n", dp[N][M]);
} 

int main() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    solve();
  }
  return 0;
}

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

int dp[2000][2000];

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n; cin >> n;
  while(n--){
    string sa, sb; cin >> sa >> sb;
    memset(dp, 0, sizeof(dp));
    for(int i = 1; i <= sa.size(); i++){
      for(int j = 1; j <= sb.size(); j++){
        if(sa[i] == sb[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[sa.size()][sb.size()] << endl;
  }
  return 0;
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

short checkTable[1001][1001];

int tmp_max=0;


void calc(int index,char pattern[],char to_search[],int to_search_length){

	for(int i=to_search_length-1;i>=0;i--){
		if(to_search[i]==pattern[index]){
			checkTable[index][i]=1;
			if(pattern[index+1]!='\0' && i < to_search_length-1){
				checkTable[index][i] = checkTable[index+1][i+1] + checkTable[index][i];
			}
		}else{
			if(i != to_search_length-1){
				if(pattern[index+1] != '\0'){
					checkTable[index][i]=(checkTable[index+1][i]>checkTable[index][i+1])
							?checkTable[index+1][i]:checkTable[index][i+1];

				}else{
					checkTable[index][i]=(checkTable[index][i+1]>0)?checkTable[index][i+1]:0;
				}
			}else{
				if(pattern[index+1] != '\0'){
					checkTable[index][i]=(checkTable[index+1][i]>0)?checkTable[index+1][i]:0;
				}else{
					checkTable[index][i] = 0;
				}
			}
		}
		tmp_max = (tmp_max >= checkTable[index][i])?tmp_max:checkTable[index][i];
	}
}

int main(){

	int q,left_length,right_length;
	char left[1001],right[1001];

	scanf("%d",&q);

	for(int i=0;i<q;i++){
		left_length=0;
		right_length=0;
		scanf("%s %s",left,right);
		for(int k=0;left[k]!='\0';k++)left_length++;
		for(int k=0;right[k]!='\0';k++)right_length++;

		if(right_length>=left_length){

			for(int k=left_length-1;k>=0;k--)calc(k,left,right,right_length);
		}else{

			for(int k=right_length-1;k>=0;k--)calc(k,right,left,left_length);
		}
		printf("%d\n",tmp_max);
		tmp_max=0;
	}

    return 0;
}

C++: #include <iostream>
#define MAX 1010

int C[MAX][MAX];

int main() {
  int n;
  std::cin >> n;
  int answers[160];

  for(int m=0; m<n; ++m) {
    std::string X;
    std::string Y;
    std::cin >> X;
    std::cin >> Y;

    // initialize
    for(int k=0; k<MAX; ++k) {
      for(int j=0; j<MAX; ++j) C[k][j] = 0;
    }

    // lcs
    int xlen = X.size();
    int ylen = Y.size();

    for(int i=1; i<=xlen; ++i) {
      for(int j=1; j<=ylen; ++j) {
        if(X[i] == Y[j]) {
          C[i][j] = C[i-1][j-1] + 1;
        } else {
          C[i][j] = std::max(C[i-1][j], C[i][j-1]);
        }
      }
    }

    answers[m] = C[xlen][ylen];
  }

  for(int h=0; h<n; ++h) {
    std::cout << answers[h] << std::endl;
  }
}