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>
#define FOR(i,n) for(int i=0;i<(int)(n);i++)
#define FORR(i,m,n) for(int i=(int)(m);i<(int)(n);i++)
#define pb(a) push_back(a)
#define ZERO(a) memset(a,0,sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define INF 1<<29
#define MAX 
using namespace std;

int dp[4444][4444];

void solve(){
 string s,t;
 while(scanf("%s",&s)){
  scanf("%s",&t);
  FOR(i,s.size()){
   FOR(j,t.size()){
    if(s[i]==t[j]){
     dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
    }
   }
  }
  int ma=0;
  FOR(i,s.size()+1){
   FOR(j,t.size()+1){
    ma=max(ma,dp[i][j]);
   }
  }
  printf("%d",ma);
 }
}

int main(){
 solve();
 return 0;
}

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

int main()
{
	string S, T;
	while (cin >> S >> T) {
		int a = S.size(), b = T.size();
		vector<vector<int>> dp(a + 1, vector<int>(b + 1));
		int res = 0;
		for (int i = 0; i < a; i++) {
			for (int j = 0; j < b; j++) {
				dp[i + 1][j + 1] = S[i] == T[j] ? dp[i][j] + 1 : 0;
				res = max(res, dp[i + 1][j + 1]);
			}
		}
		cout << res << endl;
	}
	return 0;
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define vi vector<int>
#define VS vector<string>
#define all(x) x.begin(),x.end()
#define mp make_pair
#define pb push_back

using namespace std;

int main(){
    string n, m;
    while(cin >> n >> m){
        int ans = 0;
        if(n.size() > m.size()) swap(n, m);
        for(int i=0; i < n.size(); i++){
            for(int j=i+1; j < n.size(); j++){
                if(j-i <= ans) continue;
                string s = n.substr(i, j-i);
                if(m.find(s) != string::npos){ans = max(ans, (int)s.size());}
            }
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

string a, b;
set<string> can, can_not;
int ans = 0;
int dp[ 4001 ][ 4001 ];

int main() {
	while ( cin >> a ) {
		cin >> b;
		CLR( dp );
		ans = 0;
		if ( a.size() > b.size() ) {
			swap( a, b );
		}
		for ( int i = 0; i < a.size(); i++ ) {
			for ( int j = 0; j < b.size(); j++ ) {
				if ( a[ i ] == b[ j ] ) {
					if ( !i || !j ) {
						dp[ i ][ j ] = 1;
					}
					else {
						dp[ i ][ j ] = dp[ i - 1 ][ j - 1 ] + 1;
					}
				}
				ans = max( ans, dp[ i ][ j ] );
			}
		}
		printf( "%d\n", ans );
		can.clear();
		can_not.clear();
	}
	return 0;
}

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

using namespace std;

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

C++: #include<cstdio>
#include<cstring>

int main()
{
        char r[2][4002] = {};
        while (gets (r[0])) {
                gets (r[1]);
                int d[4002][4002] = {}, le, ri, a = 0;
                le = strlen (r[0]), ri = strlen (r[1]);
                for (int i=1; i<=le; i++) {
                        for (int j=1; j<=ri; j++) {
                                if (i==1 || j==1) d[i][j] = 1;
                                else if (r[0][i-1] == r[1][j-1]) d[i][j] = d[i-1][j-1] + 1;
                                else d[i][j] = 0;
                                a = a>d[i][j] ? a:d[i][j];
                        }
                }
                printf ("%d\n", a);
        }
        return 0;
}

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

int main()
{
	while (true){
		string s, t;
		cin >> s;
		if (!s.size()) break;
		cin >> t;
		int dp1[4001] = { 0 }, dp2[4001] = { 0 }, mx = 0;
		for (int i = 0; i < s.size(); i++){
			for (int j = 0; j < t.size(); j++){
				if (s[i] == t[j]){
					dp2[j + 1] = dp1[j] + 1;
					mx = max(mx, dp2[j + 1]);
				}
			}
			for (int j = 0; j <= t.size(); j++){
				dp1[j] = dp2[j];
				dp2[j] = 0;
			}
		}
		cout << mx << endl;
	}
	return 0;
}

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

#define MAX 4000

using namespace std;

int dp[MAX + 2][MAX + 2];

int main(void){

  string s,t;
  while(cin >> s >> t){

    fill(dp[0], dp[MAX + 1] + MAX + 2, 0);

    for(int i = 0 ; i < s.size() ; i++){
      for(int j = 0 ; j < t.size() ; j++){
	if(s[i] == t[j]) dp[i + 1][j + 1] += dp[i][j] + 1;
	else dp[i + 1][j + 1] = 0;
      }
    }
    int ans = 0;
    for(int i = 0 ; i < s.size() ; i++){
      for(int j = 0 ; j < t.size() ; j++){
	ans = max(dp[i][j], ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;

string in[2];

void solve()
{
	int res = 0;
	if(in[0].size() > in[1].size())
		swap(in[0],in[1]);

	//for(int u = 0; u < 2; u++){
		for(int i = 0; i < in[0].length(); i++){
			for(int j = 0; j < in[1].length(); j++){
				if(in[0][i] == in[1][j]){
					int row = 1;
					for(int k = 1; i+k < in[0].length() && j+k < in[1].length(); k++,row++){
						if(in[0][i+k] != in[1][j+k])
							break;
					}
					res = max(res,row);
				}
			}
		}
	//	swap(in[0],in[1]);

//	}

	cout << res << endl;
}

int main(void)
{
	while(cin >> in[0] >> in[1], !cin.eof())
		solve();

	return 0;
}