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 <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    string s1, s2;
    while (cin>>s1>>s2) {
        int res=0;
        for(int off = -(int)s1.size(); off <= (int)s2.size(); off++) {
            int len = 0;
            for(int i = max(off, 0); i < min(s1.size()+off, s2.size()); i++) {
                if(len > res) {
                    res = len;
                }
                if(s1[i-off] != s2[i]) {
                    len = 0;
                } else {
                    len++;
                }
            }
        }
        cout << res << endl;
    }
}
/*
char str1[4001], str2[4001];
int main() {
    while (scanf("%s%s",&str1,&str2)!=EOF) {
        int res=0;
        for (int off=-1*(int)strlen(str1); off<=(int)strlen(str2); off++) {
            int len=0;
            for (int i=max(off,0); i<min(strlen(str1)+off,strlen(str2)); i++) {
                if (len>res) res=len;
                if (str1[i-off]!=str2[i]) len=0;
                else len++;
            }
        }
        printf("%d\n",res);
    }
}
*/

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

main() {
  while(1) {
    string str[2];
    getline(cin, str[0]);
    getline(cin, str[1]);
    if(cin.eof()) break;

    int data[str[0].length()+1][str[1].length()+1];
    int maxL = 0;
    for(int i = 0; i <= str[0].length(); i++) {data[i][0] = 0;}
    for(int j = 0; j <= str[1].length(); j++) {data[0][j] = 0;}

    for(int i = 1; i <= str[0].length(); i++) {
      for(int j = 1; j <= str[1].length(); j++) {
	if(str[0][i-1] == str[1][j-1]) data[i][j] = data[i-1][j-1]+1;
	else     data[i][j] = 0;

	if(data[i][j] > maxL) maxL = data[i][j];
      }
    }
    cout << maxL << endl;
  }
}

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

int count(string& a, string& b) {
    int a_len = a.length();
    int b_len = b.length();
    if (a_len < b_len) {
        swap(a, b);
        swap(a_len, b_len);
    }
    int max_lcs = 0;
    int cur_lcs = 0;
    for (int i = 0; i < a_len; i++) {
        for (int j = 0; j < b_len && i + j < a_len; j++) {
            if (a[i+j] == b[j]) {
                cur_lcs++;
            } else {
                if (max_lcs < cur_lcs) {
                    max_lcs = cur_lcs;
                }
                cur_lcs = 0;
            }
        }
    }
    return max_lcs;
}

int main() {
    string a, b;
    while (cin >> a >> b) {
        cout << count(a, b) << endl;
    }
    return 0;
}

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

C++: #include<bits/stdc++.h>
using namespace std;
main(){
  string a,b;
  int c,d;
  int dp[2][5000],ans;
  while(cin>>a>>b){
    c=a.size(),d=b.size();
    ans=0;
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=c;i++){
      for(int j=1;j<=d;j++){
	int ni=i%2,ri=1-i%2;
	//niは今のaの文字数が奇数か偶数か dpテーブルの使い回しのため
	//riはniじゃない方←前の結果のdpテーブル
	cout<<a[i-1]<<" "<<b[j-1]<<endl;
	if(a[i-1]==b[j-1]){//一致したら前の状態の前の文字のdpテーブルから値を引っ張ってくる
	  dp[ni][j]=dp[ri][j-1]+1;
	}
	else dp[ni][j]=0;//違うなら打ち切る
	ans=max(ans,dp[ni][j]);
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;


int main(){
  while(true){
    char s[4001],t[4001];
    int l,r,ans = 0;
    static int dp[4001][4001];
    if(scanf("%s %s",s,t)==EOF) return 0;
    l = strlen(s), r = strlen(t);
    for(int i=0;i<=l;i++){
      for(int j=0;j<=r;j++){
        if(i==0||j==0) dp[i][j] = 0;
        else if(s[i-1]==t[j-1]){
          dp[i][j] = dp[i-1][j-1] + 1;
          ans = max(ans,dp[i][j]);
        }else{
          dp[i][j] = 0;
        }
      }
    }
    printf("%d\n",ans);
  }
}

C++: #include "stdio.h"
#include "string.h"

char st1[5000];
char st2[5000];
char flg[15000];
int ans[1000];

int main()
{
  int nw = 0;
  while(1)
    {
      if(NULL == gets(st1))
	{
	  break;
	}
      gets(st2);
      int st1l,st2l;
      st1l = strlen(st1);
      st2l = strlen(st2);
      int m = 0;
      for(int i = 0; i < st1l + st2l; i++)
	{
	  for(int ii = 0; ii < 15000; ii++)
	    {
	      flg[ii] = 1;
	    }
	  for(int ii = 0; ii < st1l; ii++)
	    {
	      flg[ii + st2l] = st1[ii];
	    }
	  for(int ii = 0; ii < st2l; ii++)
	    {
	      if(flg[i + ii] == st2[ii])
		{
		  flg[i + ii] = 0;
		}
	      else
		{
		  flg[i + ii] = 1;
		}
	    }
	  int n = 0;
	  for(int ii = 0; ii < 15000; ii++)
	    {
	      if(flg[ii] == 0)
		{
		  n++;
		  if(n > m)
		    {
		      m = n;
		    }
		}
	      else
		{
		  n = 0;
		}
	    }
	}
      ans[nw] = m;
      nw++;
    }
  for(int i = 0; i < nw; i++)
    {
      printf("%d\n",ans[i]);
    }
  return 0;
}

C++: //JOI 0528 Common Sub-String
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	while(1){
		string s, t; int roop1=0, roop2 = 0, save[600500000]={}, 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;
}