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++: //JOI 0528 Common Sub-String
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	while (cin){
		string s, t; int roop1, roop2 = 0, save[10000] = {}, ans = 0;
		cin >> s;
		cin >> 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++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    string s,t;
    while(cin>>s>>t){
        int dp[2][4001]={{0}};
        int Max=0;
        for(int i=0;i<t.length();i++){
      
            for(int j=0;j<s.length();j++){
                if(t[i]==s[j])dp[(i+1)&1][j+1]=dp[i&1][j]+1;
                else dp[(i+1)&1][j+1]=0;

                Max=max(dp[(i+1)&1][j+1],Max);
              
            }
    

        }
        cout<<Max<<endl;
    }
}

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[4002][4002];
int main(void){
   char a[4002],b[4002];
   while(scanf("%s",a+1) != EOF){
      scanf("%s",b+1);
      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 <string>
#include <algorithm>
using namespace std;

string s,t;
int main() {
	while(cin>>s>>t){
		int ans=0;
		for(int i=0;i<s.size();i++){
			for(int j=0;j<t.size();j++){
				if(s[i]==t[j]){
					int sum=1;
					for(int k=1;k<min(s.size()-i,t.size()-j);k++){
						if(s[i+k]==t[j+k]){
							sum++;
						} else{
							ans=max(ans,sum);
							if(ans>=s.size()-i){
								goto goal;
							}
							break;
						}
					}
				}
			}
		}
		
goal:
		
		cout<<ans<<endl;
	}
	return 0;
}

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 4002
using namespace std;

int dp[MAX][MAX];

void solve(){
 string s,t;
 while(~scanf("%s",&s)){
  scanf("%s",&t);
  ZERO(dp);
  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<string>
#include<iostream>

using namespace std;

int main()
{
	for(string s1,s2;cin>>s1>>s2;){
		int len1=s1.length(),len2=s2.length();
		int lcslen=0;
		for(int i=-len2+1;i<len1+len2;i++){
			int cnt=0;
			for(int j=0;j<len1;j++){
				if(0<=i+j && i+j<len2 && s1[j]==s2[i+j])	cnt++;
				else	lcslen=max(lcslen,cnt),cnt=0;
			}
			lcslen=max(lcslen,cnt);
		}
		cout<<lcslen<<endl;
	}

	return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<ll, P> E;
#define MOD (1000000007ll)
#define l_ength size
#define PI 3.14159265358979
 
void mul_mod(ll& a, ll b){
    a *= b;
    a %= MOD;
}
 
void add_mod(ll& a, ll b){
    b += MOD;
    a += b;
    a %= MOD;
}

int n,m;
ll memo[4444][4444];
bool done[4444][4444];
char s[4444],t[4444];

ll dp(int i, int j){
    if(done[i][j]){
        return memo[i][j];
    }
    done[i][j] = true;
    if(i == n || j == m){
        memo[i][j] = 0ll;
    }else if(s[i] != t[j]){
        memo[i][j] = 0ll;
    }else{
        memo[i][j] = dp(i+1,j+1) + 1;
    }
    return memo[i][j];
}

int main(void){
    int i,j;
    ll ans;
    while(scanf("%s",s)!=EOF){
        fill(done[0],done[4444],false);
        ans = 0ll;
        scanf("%s",t);
        n = strlen(s); m = strlen(t);
        for(i=0; i<n; ++i){
            for(j=0; j<m; ++j){
                ans = max(ans,dp(i,j));
            }
        }
        cout << ans << endl;
    }
    return 0;
}

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

int dp[4001][4001];

int main(){
    string a, b;
    while(cin >> a >> b){
        int ans = 0;

        for(int i = 0; i < a.length(); i++){
            for(int j = 0; j < b.length(); j++){
                dp[i][j] = 0;
            }
        }

        for(int j = 0; j < b.length(); j++) if(a[0] == b[j])    dp[0][j] = 1;
        for(int i = 0; i < a.length(); i++) if(a[i] == b[0])    dp[i][0] = 1;

        for(int i = 1; i < a.length(); i++){
            for(int j = 1; j < b.length(); j++){
                if(a[i] == b[j]){
                    dp[i][j] = dp[i-1][j-1]+1;
                    ans = max(ans, dp[i][j]);
                }
                // add this line if you want to get longest common substring
                //dp[i][j] = max(dp[i][j], max(dp[i-1][j], dp[i][j-1]));
            }
        }
        cout << ans << endl;
    }
}

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

int cnt[4001][4001];

int main(){
	string a, b;
	int na, nb;

	for(;;){
		if(cin >> a >> b){
			na = a.length();
			nb = b.length();

			for(int i=0; i<na+1; i++){
				cnt[i][0] = 0;
			}
			for(int j=0; j<nb+1; j++){
				cnt[0][j] = 0;
			}

			int mx = 0;
			for(int i=0; i<na; i++){
				for(int j=0; j<nb; j++){
					if(a[i]==b[j]){
						cnt[i+1][j+1] = cnt[i][j] + 1;
						if(mx < cnt[i+1][j+1]){
							mx = cnt[i+1][j+1];
						}
					}else{
						cnt[i+1][j+1] = 0;
					}
				}
			}
			cout << mx << endl;
		}else{
			break;
		}
	}
	return 0;
}

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

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

  string str1, str2;
  int ans;
  while(cin >> str1 >> str2){
    ans  = 0;
    // cout << str1 << endl << str2 << endl;
    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;
          ans = max(ans, dp[i][j]);
        }else dp[i][j] = 0;
      }
    }
    cout << ans << endl;
  }
}

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