Go: ///
// File:  0528.go
// Author: ymiyamoto
//
// Created on Thu Mar 22 01:52:09 2018
//
package main

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

var S, T string
var dp [4001][4001]int

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for {
		scanner.Scan()
		S = scanner.Text()
		if len(S) == 0 {
			return
		}

		scanner.Scan()
		T = scanner.Text()

		for i := range dp {
			for j := range dp[i] {
				dp[i][j] = 0
			}
		}

		dp[0][0] = 0
		for i := range S {
			for j := range T {
				if S[i] == T[j] {
					dp[i+1][j+1] = max(dp[i+1][j], max(dp[i][j+1], dp[i][j]+1))
				} else {
					dp[i+1][j+1] = 0
				}
			}
		}
		var m int
		for i := range dp {
			for j := range dp[i] {
				m = max(m, dp[i][j])
			}
		}
		fmt.Println(m)
	}
}

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

int dp[4001][4001];
 
int main(){

  string str1, str2;
 
  while(cin >> str1 >> str2){
   
 
    memset(dp,0,sizeof(dp));
 
    for(int i = 1; i <= str1.size(); i++){
      for(int j = 1; j <= str2.size(); j++){
        if(str1[i-1] == str2[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[str1.size()][str2.size()] << endl;
 
  }
}

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

int main(){
    while(true){
        string str,test;
        cin>>str;
        if(str.size()==0) break;
        cin>>test;
        vector<string> v;
        int N=str.size();
        for(int i=0;i<N;i++){
            v.push_back( str.substr(i,N-i) );
        }
        sort(v.begin(),v.end());
        int ans;
        for(int i=1;i<test.size();i++){
            bool flag=false;
            for(int j=0;j<test.size()-i+1;j++){
                string tmp = test.substr(j,i);
                vector<string>::iterator itr = lower_bound(v.begin(),v.end(),tmp);
                int z=(int)(itr-v.begin());
                if(z!=N && v[z].size() >= i){
                    bool fff=true;
                    for(int k=0;k<i;k++){
                        if(tmp[k] != v[z][k]){
                            fff=false;
                            break;
                        }
                    }
                    if(fff){
                        flag=true;
                        break;
                    }
                }
            }
            if(!flag){
                ans=i-1;
                break;
            }
        }
        cout<<ans<<endl;
    }
    
    return 0;
}