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 r(i,n) for(int i=0;i<n;i++)
using namespace std;
string s,t;
set<string>st;
int main(){
	while(cin>>s>>t){
	st.clear();
	r(j,s.size())if(j)r(i,s.size())if(j+i<=s.size())
	  st.insert(s.substr(i,j));
	for(int j=t.size()-1;j>=1;j--)if(j)r(i,t.size())if(j+i<=t.size())
		if(st.count(t.substr(i,j))){
			cout<<j<<endl;
			goto L;
		}
	cout<<0<<endl;L:;
	}
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
int INF=1e9;
int MOD=1000000007;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
double EPS = 1e-10;
const int mod=1e9+7;
int dp[4000][4000];
int main(){
  string s,t;
  while(cin>>s>>t){
    int ans=0;
    memset(dp,0,sizeof(dp));
    REP(i,s.size()){
      REP(j,t.size()){
        if(s[i]==t[j]){
          if(i==0||j==0)dp[i][j]=1;
          else dp[i][j]=dp[i-1][j-1]+1;
        }
        ans=max(ans,dp[i][j]);
      }
    }
    cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	string s, t; int roop1, roop2 = 0, save[10000], ans = 0;
	//while(cin){
	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++: import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
for(;sc.hasNext();){
String s=sc.next();
String t=sc.next();
System.out.println(lcs(s,t).length());
}
}

	static String lcs(String s, String t){
		int m=s.length(), n=t.length();
		int[][] d=new int[n+1][m+1];
		int[][] v=new int[n+1][m+1];
		for(int j=1; j<=n; j++){
			for(int i=1; i<=m; i++){
				if(s.charAt(i-1)==t.charAt(j-1)){
					d[j][i]=d[j-1][i-1]+1;
					v[j][i]=0;
				}else if(d[j][i-1]>d[j-1][i]){
					d[j][i]=d[j][i-1];
					v[j][i]=+1;
				}else{
					d[j][i]=d[j-1][i];
					v[j][i]=-1;
				}
			}
		}
		String r="";
		for(int i=m, j=n; i>0&&j>0;){
			debug(i, j);
			if(v[j][i]>0)
				i--;
			else if(v[j][i]<0)
				j--;
			else{
				r=s.charAt(i-1)+r;
				i--;
				j--;
			}
		}
		return r;
	}

}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int dp[4002][4002]={};
int main() {
  string s,t;
  while( cin >> s >> t) {
    for(int i=0;i<=s.size();i++)
      for(int j=0;j<=t.size();j++)dp[i][j] = 0;
  int ans   = 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;
      ans = max(ans,dp[i+1][j+1]);
    }
  cout << ans <<endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
short dp[4001][4001]={};
char str1[4005],str2[4005];
short ans=0;
int main(){
	while(scanf("%s",str1)!=EOF){
		scanf("%s",str2);
  printf("%d\n",ans); }return 0;}

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

int main(){
	string s1,s2;
	while(cin >> s1){
		cin >> s2;
		int ma = 0;
		for(int i = 0;i < s1.size();i++){
			int cnt = 0;
			for(int j = 0; j < min(s1.size() - i,s2.size());j++){
				if(s1[j + i] == s2[j]) ma = max(ma,++cnt);
				else cnt = 0;
			}
		}
		for(int i = 0;i < s2.size();i++){
			int cnt = 0;
			for(int j = 0; j < min(s2.size() - i,s1.size());j++){
				if(s2[j + i] == s1[j]) ma = max(ma,++cnt);
				else cnt = 0;
			}
		}
		cout << ma << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;

int main(){
	char s1[100],s2[100];
	
	while(~scanf("%s %s",s1,s2)){
		int maxcom=0;
		
		for(int i=0;i<strlen(s1);i++){
			for(int j=0;j<strlen(s2);j++){
				if(s1[i]!=s2[j])continue;
				int tmax=0;
				for(int k=0;i+k<strlen(s1) && j+k<strlen(s2);k++){
					if(s1[i+k]==s2[j+k])++tmax;
					else break;
				}
				maxcom=max(maxcom,tmax);
			}
		}
		
		cout<<maxcom<<endl;
	}
}

C++: #include<algorithm>
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int i,j,k;
  string s,t;
  while(cin>>s){
    cin>>t;
    int sln=s.length(),tln=t.length();
    int mx=0;
    for(i=0;i<sln;i++){
      for(j=0;j<min(sln-i,tln);j=k){
	for(;j<min(sln-i,tln)&&s[i+j]!=t[j];j++);
	for(k=j;k<min(sln-i,tln)&&s[i+k]==t[k];k++);
	mx=max(mx,k-j);
      }
    }
    for(i=0;i<tln;i++){
      for(j=0;j<min(tln-i,sln);j=k){
	for(;j<min(tln-i,sln)&&t[i+j]!=s[j];j++);
	for(k=j;k<min(tln-i,sln)&&t[i+k]==s[k];k++);
	mx=max(mx,k-j);
      }
    }
    cout<<mx<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  string str;
  while(cin>>str){
    string str2;cin>>str2;
    if(str.size()>str2.size()){
      string tmp=str;
      str=str2;
      str2=tmp;
    }
    int ans=0;
    for(int i=str.size();i>0;i--){
      for(int j=0;j<str.size()-i;j++){
	for(int k=0;k<str2.size()-i;k++){
	  if((str.substr(j,i)).compare((str2.substr(k,i)))==0){
	    ans=i;
	    break;
	  }
	}
	if(ans)break;
      }
      if(ans)break;
    }
    cout<<ans<<endl;
  }
  return 0;
}

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

int map[4003][4003]={};

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