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

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()

		dp := make([][]int, len(S)+1)
		for i := range dp {
			dp[i] = make([]int, len(T)+1)
		}

		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<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    string A,B;
    int ans=0;
    while(cin>>A>>B){
        if(A.size()<B.size())swap(A,B);
        for(int i=-(int)(B.size())+1;i<(int)A.size();i++){
            int cou=0;
            for(int j=0;j<B.size();j++){
                if(0<=i+j&&i+j<A.size()){
                    if(A[i+j]==B[j])cou++;
                    else cou=0;
                    ans=max(ans,cou);
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

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++;
                    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);
        }
        for(int i=0;i<bsize;i++){
            int miat=min(asize,bsize-i);
            bool is=false;
            int now=0;
            for(int j=0;j<miat;j++){
                if(!is&&a[j]==b[i+j]){
                    now++;
                    is=true;
                }else if(is&&a[j]==b[i+j])now++;
                else if(is&&a[j]!=b[i+j]){
                    ma=max(ma,now);
                    now=0;
                    is=false;
                }
            }
            ma=max(now,ma);
        }
        printf("%d\n",ma);
    }
}

C++: #include<iostream>
 
using namespace std;
 
int solve(const string& s1, const string& s2, int l1, int l2){
  int tmp = 0;
  int res = 0;
  int len = min(s2.length()-l2,s1.length()-l1);
  for(int i = 0; i < len; i++){
    if(s1[l1+i] != s2[l2+i]){
      res = max(res,tmp);
      tmp = 0;
    }else{
      tmp++;
    }
  }
  return max(tmp,res);
}
 
int main(){
   
  string s1,s2;
  int l1,l2,ans;
  while(1) {
  	if(cin.eof()) {
  		break;
  	}
  	cin >> s1;
  	cin >> s2;
  	l1 = s1.length();
  	l2 = 0;
  	ans = 0;
  	while(l1 != 0 || l2 <= s2.length()) {

      if(l1 != 0){
      	l1--;
      } else {
      	l2++;
      }
      ans = max(ans,solve(s1,s2,l1,l2));
  	}
    cout << ans << endl;
  }
}

C++: #define _USE_MATH_DEFINES
#define INF 100000000
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-8;

short dp[4001][4001];

int main(){
	string str1;
	while(cin>> str1){
		memset(dp,0,sizeof(dp));

		string str2;
		cin >> str2;

		short res = 0;
		for(int i=0;i<str1.size();i++){
			for(int j=0;j<str2.size();j++){
				if(str1[i] == str2[j]){
					dp[i+1][j+1] = dp[i][j]+1;
					res = max(res,dp[i+1][j+1]);
				}
				else{
					dp[i+1][j+1] = 0;
				}
			}
		}

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

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	string s, t; int roop1, roop2 = 0, save[10000], ans = 0;
	while(1){
	cin >> s >> t;
	if(s=="")
	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++;
							cout << save[roop2] << ' ';
						}
						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<cstdio>
#include<cstring>

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

C++: // 2011/09/29 Tazoe

#include <iostream>
#include <string>
using namespace std;

int main()
{
	while(true){
		string s1;
		cin >> s1;
		if(cin.eof())
			break;

		string s2;
		cin >> s2;

		if(s1.size()>s2.size()){
			string tmp = s1;
			s1 = s2;
			s2 = tmp;
		}

		int I = 0;
		int J = 1;
		while(true){
			string tmp = s1.substr(I, J);
//			cout << tmp << endl;
			if(s2.find(tmp)==string::npos)
				I++;
			else
				J++;

			if(I+J>s1.size()){
				cout << J-1 << endl;
				break;
			}
		}
	}

	return 0;
}

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

string in[2];

void solve()
{
	int c;
	int ans = 0;

	for(int u = 0; u < 2; u++){
	for(int i = 0; i < in[0].size(); i++){
		c = 0;
		int j = 0;
		while(j + 1 < in[1].size() && in[0][i] != in[1][j])j++;
		while(i+c < in[0].size() && j+c < in[1].size() && in[0][i+c] == in[1][j+c]){
			c++;
		}

		if(c > 0)
			c++;
		ans = max(ans, c);

	
	}
		swap(in[0],in[1]);
	}
	cout << ans << endl;
}


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

C++: #include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
typedef pair<LL, LL> PLL;

short dp[4000][4000];
char s[4001], t[4001];

int main() {
    while (cin >> s >> t) {
        for (int j = 0; j < 4000; ++j) {
            fill(dp[j], dp[j] + 4000, 0);
        }

        int sLen = strlen(s);
        int tLen = strlen(t);

        int ret = 0;

        for (int j = 0; j < sLen; ++j) {
            for (int k = 0; k < tLen; ++k) {
                if (s[j] != t[k]) {
                    dp[j][k] = 0;
                } else if (j == 0 || k == 0) {
                    dp[j][k] = 1;
                } else {
                    dp[j][k] = dp[j-1][k-1] + 1;
                }

                ret = max(ret, dp[j][k]);
            }
        }

        cout << ret << endl;
    }
    return 0;
}