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] = 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 <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

string A,B;
int lenA,lenB;

int dp[4096][4096];

int main(){
  while(cin >> A >> B){
    lenA = A.size(), lenB = B.size();
    memset(dp, 0, sizeof(dp));
    for(int i=0;i<lenA;i++){
      for(int j=0;j<lenB;j++){
	if(A[i]==B[j]) dp[i+1][j+1] = dp[i][j]+1;
      }
    }
    int res = 0;
    for(int i=0;i<=lenA;i++) for(int j=0;j<=lenB;j++) res = max(res, dp[i][j]);
    cout << res << endl;
  }
  return 0;
}

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

int solve(const string& s1, const string& s2)
{
  int max_idx1 = 0;
  int max_idx2 = 0;
  int max_len = 0;
  for (int i = -int(s1.size()); i <= int(s2.size()); i++) {
    int len = 0;
    for (int j = max(i, 0); j < int(min(s1.size()+i, s2.size())); j++) {
      if (s1[j-i] == s2[j]) {
        len++;
        if (len > max_len) {
          max_idx1 = j-i-len+1;
          max_idx2 = j-len+1;
          max_len = len;
        }
      } else {
        len = 0;
      }

    }
  }
  return max_len;
}

int main()
{
  string s1, s2;
  while (cin >> s1 >> s2) {
    cout << solve(s1, s2) << endl;
  }
  return 0;
}

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

using namespace std;

int main() {
	string a, b;
	while (cin >> a >> b) {
		int MAX = 0;
		for (int i = 0; i < a.size(); i++) {
			int c = 0;
			for (int j = 0; j < b.size(); j++) {
				if (i+j < a.size()) {
					if (a[i+j] == b[j]) {
						c++;
					} else {
						MAX = max(MAX, c);
						c = 0;
					}
				} else {
					MAX = max(MAX, c);
					break;
				}
			}
		}
		cout <<MAX << endl;
	}
	return 0;
}

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

int main(void){
  char ra[4005]={0};
  char rb[4005]={0};
  while(scanf("%s%s",ra,rb)!=EOF){
    int alen=strlen(ra);
    int blen=strlen(rb);
    //printf("%d\n%d\n",alen,blen);
    int i=0;
    int j=0;
    int k=0;
    int km=0;
    int ans=0;
    for(i=0;i<alen;i++){
      for(j=0;j<blen;j++){
	if(ra[i]==rb[j]){
	  k=0;
	  km=-1;
	  while(k<blen-j && k<alen-i && ra[i+k]==rb[j+k]){
	    k++;
	    if(km==-1 && ra[i]==rb[j+k])km=k;
	  }
	  ans=(ans<k)?k:ans;
	  if(km!=-1){
	    j+=km-1;
	  }
	  else{
	    j+=k-1;
	  }
	}
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

int dp[4001][4001];

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S, T;
    while(cin >> S >> T){
        int ans = 0;
        int tmp1 = 0;
        rep(i, S.length()) {
            rep(j, T.length()) {
                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]);
                else dp[i + 1][j + 1] = dp[i][j + 1];
                tmp1 = max(tmp1, dp[i + 1][j + 1]);
            }
        }
        int tmp2 = 0;
        rep(i, S.length()) {
            rep(j, T.length()) {
                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]);
                else dp[i + 1][j + 1] = dp[i + 1][j];
                tmp2 = max(tmp2, dp[i + 1][j + 1]);
            }
        }
        /*
        rep(i, S.length() + 1) {rep(j, T.length() + 1)
            cout << "dp[" << i << "][" << j << "] = " << dp[i][j] << endl;
        //*/
        /*
        rep(i, S.length() + 1) {
            cout << i << " ";
            rep(j, T.length() + 1) {
                cout << dp[i][j] << " ";
            }
            cout << endl;
        }
        //*/
           

        cout << min(tmp1, tmp2) << endl;
    }


    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
const int N = 4000;
int memo[N][N];
int dp[N][N];

/*
  find substr of txt from word
 */
int solve(string &txt,string &word){
  rep(i,txt.size())rep(j,word.size())dp[i][j] = 0,memo[i][j] = 0;
  rep(i,word.size())if (txt[0] == word[i])dp[0][i] = 1;

  REP(i,1,txt.size()){
    rep(j,word.size()){
      if (txt[i] == word[j]){
	dp[i][j] = 1;
	if (j-1 >= 0)dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
      }else dp[i][j] = 0;
    }
  }
  int ret = 0;
  rep(i,txt.size())rep(j,word.size())ret = max(ret,dp[i][j]);
  return ret;
}

main(){
  string a,b;
  while(cin>>a>>b){
    cout << solve(a,b)<<endl;
  }
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
string str1;
string str2;
int main(){
	while(cin>>str1>>str2){
		int ans = 0;
		for(int i = 0;i <str1.length();i++){
			for(int j=0;j <str1.length();j++){
				if(i>0&&j>0&&str1[i-1]==str2[j-1])continue;
				int tmp = 0;
				for(int k = 0;k+i<str1.length()&&k+j<str2.length();k++){
					if(str1[i+k]!=str2[j+k]){
						break;
					}
					tmp++;
				}
				ans =max(ans,tmp);
			}
		}
		cout <<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 100000005
#define MAX 5001
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int dp[4005][4005];
int main(){
	string s1, s2;
	while(cin >> s1 >> s2, s1 != ""){
	s1 = " " + s1;s2 = " " + s2;
	fill((int*)dp, (int*)(dp+4005), 0);
	int ans = 0;
	for(int i = 1;i <= s1.size();i++){
		for(int j = 1;j <= s2.size();j++){
			if(s1[i] == s2[j])dp[i][j] = dp[i-1][j-1]+1;
			ans = max(ans, dp[i][j]);
		}
	}
	cout << ans << endl;
	}
	return 0;
}