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 <stdio.h>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

int T_char_count[128] = {0};

class LocRecord{

public:
	LocRecord(){
		locationTable = new int*[128];
		for(int i = 65; i <= 90; i++){
			if(T_char_count[i] > 0){
				locationTable[i] = new int[T_char_count[i]+1];
			}
		}

		int limit;
		for(int i = 65; i <= 90; i++){
			limit = T_char_count[i];
			index[i] = 0;
			if(T_char_count[i] > 0){
				for(int k = 0; k <= limit; k++){
					locationTable[i][k] = -1;
				}
			}
		}
	}

	void init_index(){
		for(int i = 65; i <= 90; i++){
			index[i] = 0;
		}
	}

	void regist_loc(int ch, int location){
		locationTable[ch][index[ch]] = location;
		index[ch]++;
	}

	int next_loc(int ch){
		if(T_char_count[ch] == 0)return -1;

		int ret = locationTable[ch][index[ch]];
		index[ch]++;
		return ret;
	}

private:
	int** locationTable;
	int index[128];
};


int main(){

	int count,left_length,right_length,max,tmp_len,start_loc;
	char left[4001],right[4001];

	while(scanf("%s\n%s\n",left,right)!=EOF){

		for(int i = 65; i <= 90; i++)T_char_count[i] = 0;
		max = 0;

		for(left_length = 0; left[left_length] != '\0';left_length++);
		for(right_length = 0; right[right_length] != '\0';right_length++);

		if(left_length > right_length){

			for(int i = 0; i < left_length; i++)T_char_count[left[i]]++;

			LocRecord locRecord;

			for(int i = 0; i < left_length; i++)locRecord.regist_loc(left[i],i);

			for(int i = 0; i < right_length; i++){
				locRecord.init_index();
				while((start_loc = locRecord.next_loc(right[i])) != -1){
					tmp_len = 0;
					for(int k = 0; start_loc + k < left_length; k++){
						if(right[i+k] == left[start_loc+k])tmp_len++;
						else{
							max = (max >= tmp_len)?max:tmp_len;
							break;
						}
					}
				}
			}

			printf("%d\n",max);

		}else{

			for(int i = 0; i < right_length; i++)T_char_count[right[i]]++;

			LocRecord locRecord;

			for(int i = 0; i < right_length; i++)locRecord.regist_loc(right[i],i);

			for(int i = 0; i < left_length; i++){
				locRecord.init_index();
				while((start_loc = locRecord.next_loc(left[i])) != -1){
					tmp_len = 0;
					for(int k = 0; start_loc + k < left_length; k++){
						if(left[i+k] == right[start_loc+k])tmp_len++;
						else{
							max = (max >= tmp_len)?max:tmp_len;
							break;
						}
					}
				}
			}

			printf("%d\n",max);
		}
	}

    return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
#define REP(i,a,n) for(int i = a;i < n;i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
int field[4000][4000];
int main(void){
   char a[4002],b[4002];
   while(scanf("%s%s",a+1,b+1) != EOF){
      int ma = 0;
      memset(field,0,sizeof(int)*4000*4000);
      a[0] = '0'; b[0] = '1';
      REP(i,1,strlen(a)){
         REP(j,1,strlen(b)){
            if(a[i] == b[j]){
               field[i][j] = field[i-1][j-1]+1;
               ma = max(ma,field[i][j]);
            }
         }
      }

      printf("%d\n",ma);
   }
   return 0;
}

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

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

int dp[4001][4001];

int main(void){
	string s,t;
	while(cin >> s >> t){
		if(cin.eof()) break;
		int n=s.size(),m=t.size();
		rep(i,n)rep(j,m) dp[i][j]=0;
		rep(i,n)rep(j,m){
			if(s[i]==t[j])
				dp[i+1][j+1]=dp[i][j]+1;
			else
				dp[i+1][j+1]=0;
		}
		int ans=0;
		rep(i,n+1)rep(j,m+1) ans=max(ans,dp[i][j]);
		cout << ans << endl;		
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
        int roop1, roop2 = 0, save[10000], ans = 0;
	while(cin){
        string s, t; 
	cin >> s >> t;
        if(cin.eof())
        break;
	for (int i = 0; i<s.size(); i++){
		for (int j = 0; j<t.size(); j++){
			if (s[i] == t[j]){
				roop1 = 0;
				save[roop2] = 0;
				while (1){
					if (s[i + roop1] == 0|| t[j + roop1] ==0){
						break;
					}
					else{
					if (s[i + roop1] == t[j + roop1]){
							save[roop2] += 1;
							roop1++;
						}
						else if (s[i + roop1] != t[j + roop1]){
							roop2++;
							break;
						}
					}
				}
			}
		}
	}
	sort(save, save + roop2 + 1);
	ans = save[roop2];
	cout << ans << endl;
	}
	return 0;
}

C++: //Bokan ga bokka--nn!!
//Daily Lunch Special Tanoshii !!
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
typedef long long ll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 2000000000
#define s(x) scanf("%d",&x)
#define rep(i,x) for(int i=0;i<x;i++)
int dp[4005][4005];
char a[4005],b[4005];
int main()
{
	while(scanf("%s %s",&a,&b)!=EOF)
	{
		memset(dp,0,sizeof(0));
		
		int as=strlen(a); int bs=strlen(b);
		int ret=0;
		for(int i=1;i<=as;i++)
		{
			for(int j=1;j<=bs;j++)
			{
				if(a[i-1]==b[j-1]) dp[i][j]=max(max(dp[i][j-1],dp[i][j]),max(dp[i-1][j-1]+1,dp[i-1][j]));
				else dp[i][j]=0;
				ret=max(ret,dp[i][j]);
			}
			
		}
		printf("%d\n",ret);
	}
	
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main(){
    char a[4002],b[4002];
    while(scanf("%s\n%s\n",a,b)!=-1){
        int asize=strlen(a),bsize=strlen(b);
        int ma=0;
        for(int i=0;i<asize;i++){
            int miat=min(bsize,asize-i);
            bool is=false;
            int now=0;
            for(int j=0;j<miat;j++){
                if(!is&&a[i+j]==b[j]){
                    now=1;
                    is=true;
                }else if(is&&a[i+j]==b[j])now++;
                else if(is&&a[i+j]!=b[j]){
                    ma=max(ma,now);
                    now=0;
                    is=false;
                }
            }
            ma=max(now,ma);
        }
        printf("%d\n",ma);
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define Rep(i,N) for(int i = 0; i < N; i++)

int main()
{
  string S, W;
  int dp[4005] = {0};
  int maxv = 0;
  
  cin >> S >> W;

  Rep(i,W.size()) {
    for(int j = S.size(); j > 0; j--) {
      if(S[j - 1] == W[i]) {
	dp[j] = dp[j - 1] + 1;
	maxv = max(maxv, dp[j]);
      } else dp[j] = 0;
    }
  }

  cout << maxv << endl;
  return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
 
using namespace std;
 
constexpr int MAX = 4001;
  
int dp[MAX][MAX];
  
int main()
{
    string s1, s2;
  
    while (cin >> s1 >> s2) {
        memset(dp, 0, sizeof(dp));
  
        int n = s1.size(), m = s2.size();
        int ans = 0; 
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (s1[i] == s2[j]) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                }
                ans = max(ans, dp[i + 1][j + 1]);
            }
        }
        cout << ans << endl;
    }
    return 0;
}