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 <stdio.h>
#include <vector>
#include <algorithm>
#include <math.h>
#include <queue>

using namespace std;

int main(){

	int left_length,right_length,max,tmp,left_start,left_end,right_start,right_end,k;
	char left[4001],right[4001];

	while(scanf("%s",left) != EOF){
		scanf("%s",right);

		for(left_length = 0;left[left_length] != '\0';left_length++);
		for(right_length = 0; right[right_length] != '\0';right_length++);

		max = 0;
		if(right_length > left_length){

			//left???????????¨right???????????????????????§
			for(right_end = 1;right_end <= left_length;right_end++){
				tmp = 0;
				for(left_start = left_length-right_end,k=0; k < right_end; k++){
					if(left[left_start+k] == right[k]){
						tmp++;
						max = (max >= tmp)?max:tmp;
					}else{
						tmp = 0;
					}
				}
			}

			//left???????????¨right???????????????????????§
			for(right_end = left_length;right_end < right_length;right_end++){
				tmp = 0;
				for(right_start = right_end-left_length,k=0;right_start + k < right_end; k++){
					if(left[k] == right[right_start+k]){
						tmp++;
						max = (max >= tmp)?max:tmp;
					}else{
						tmp = 0;
					}
				}
			}

			//left_end???0??????????????§
			for(left_end = left_length-1;left_end >= 1; left_end--){
				tmp = 0;
				for(right_start = right_length-left_end,k=0;k < left_end; k++){
					if(left[k] == right[right_start+k]){
						tmp++;
						max = (max >= tmp)?max:tmp;
					}else{
						tmp = 0;
					}
				}
			}

			printf("%d\n",max);

		}else{

			//right???????????¨left???????????????????????§
			for(left_end = 1;left_end <= right_length;left_end++){
				tmp = 0;
				for(right_start = right_length-left_end,k=0; k < left_end; k++){
					if(right[right_start+k] == left[k]){
						tmp++;
						max = (max >= tmp)?max:tmp;
					}else{
						tmp = 0;
					}
				}
			}

			//right???????????¨left???????????????????????§
			for(left_end = right_length;left_end < left_length;left_end++){
				tmp = 0;
				for(left_start = left_end-right_length,k=0;left_start + k < left_end; k++){
					if(right[k] == left[left_start+k]){
						tmp++;
						max = (max >= tmp)?max:tmp;
					}else{
						tmp = 0;
					}
				}
			}

			//right_end???0??????????????§
			for(right_end = right_length-1;right_end >= 1; right_end--){
				tmp = 0;
				for(left_start = left_length-right_end,k=0;left_start + k < left_length; k++){
					if(right[k] == left[left_start+k]){
						tmp++;
						max = (max >= tmp)?max:tmp;
					}else{
						tmp = 0;
					}
				}
			}

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

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>
#include <cstdlib>
#include <cmath>

using namespace std;

#define rep(i, n)      for (int i=0; i<int(n); i++)
#define rep3(i, s, n)  for (int i=int(s); i<int(n); i++)
#define rev_rep(i, n)  for (int i=int(n)-1; i>=0; i-=1)

string s;
string t;
int dp[4010][4010];
int main() {
  while (cin >> s >> t) {
  int s_len = s.length(), t_len = t.length();
  rep(i, s_len) {
    rep(j, t_len) {
      dp[i][j] = 0;
    }
  }
  rep(i, s_len) {
    rep(j, t_len) {
      if (s[i] == t[j]) dp[i][j] = ((i>1&&j>1) ? dp[i-1][j-1] : 0) + 1;
      else              dp[i][j] = 0;
    }
  }
  int ans = 0;
  rep(i, s_len) {
    rep (j, t_len) {
      ans = max(ans, dp[i][j]);
    }
  }
  cout << ans << "\n";
  }
  return 0;
}

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

int main(){
	string s,big;
	int mi;
	string substr;
	while(cin >> s >> big){
		if(s.length()>big.length())
			swap(s,big);
		mi = 0;
		for(int m=1;m<=s.length();m++){
			substr="";
			for(int i=0;i<m;i++)substr += s[i];
			if(~big.find(substr)){mi = m;continue;}
			for(int i=m;i<s.length();i++){
				substr.erase(0,1);
				substr+=s[i];
				if(~big.find(substr)){mi = m;continue;}
			}
		}
		cout << mi << endl;
	}
}

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

int main(){
	string s,t;
	while(cin >> s >> t){
		int a=0,S=s.size(),T=t.size(),i,j;
		map<string,int> m;
		for(i=0;i<S;i++) for(j=1;j<=S-i;j++) m[s.substr(i,j)]++;
		for(i=0;i<T;i++) for(j=1;j<=T-i;j++) if(m[t.substr(i,j)]>=1) a=max(a,j);
		cout<<a<<endl;
	}
}