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 <cstdint>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <utility>
#include <queue>
#include <memory>
#include <map>

template <class String>
class matching_automaton {
  using char_type = typename String::value_type;
  struct node {
    size_t length = 0;
    std::shared_ptr<node> output_link = nullptr;
    std::shared_ptr<node> suffix_link = nullptr;
    std::map<char_type, std::shared_ptr<node>> links;
    bool accepting = false;

    std::shared_ptr<node> append(char_type c) {
      auto it = links.lower_bound(c);
      if (it != links.end() && it->first == c) return it->second;
      std::shared_ptr<node> newnode(new node);
      newnode->length = length+1;
      links.emplace_hint(it, c, newnode);
      return newnode;
    }

    std::shared_ptr<node> find(char_type c) const {
      auto it = links.find(c);
      return (it != links.end())? it->second : nullptr;
    }
  };
  std::shared_ptr<node> root;

  template <class ForwardIt>
  void build_nodes(ForwardIt first, ForwardIt last) {
    for (auto it = first; it != last; ++it) {
      std::shared_ptr<node> cur = root;
      for (char_type c: *it) cur = cur->append(c);
      cur->accepting = true;
    }
  }

  void build_links() {
    std::queue<std::shared_ptr<node>> q;
    for (const auto& p: root->links) {
      q.emplace(p.second);
      p.second->suffix_link = root;
    }

    while (!q.empty()) {
      std::shared_ptr<node> cur(q.front());
      q.pop();
      std::set<char_type> cc;
      for (const auto& p: cur->links) {
        cc.insert(p.first);
        p.second->suffix_link = root;
        q.emplace(p.second);
      }
      for (auto sf = cur->suffix_link; sf; sf = sf->suffix_link) {
        for (auto it = cc.begin(); it != cc.end();) {
          char_type c = *it;
          std::shared_ptr<node> dst(sf->find(c));
          if (!dst) {
            ++it;
            continue;
          }
          cur->find(c)->suffix_link = dst;
          it = cc.erase(it);
        }
        if (cc.empty()) break;
      }

      if (cur->suffix_link->accepting) {
        cur->output_link = cur->suffix_link;
      } else if (cur->suffix_link->output_link) {
        cur->output_link = cur->suffix_link->output_link;
      }
    }
  }

  template <class Functor>
  void match_do(const String& s, Functor fn) const {
    std::shared_ptr<node> cur(root);
    for (size_t i = 0; i < s.length(); ++i) {
      char_type c = s[i];
      std::shared_ptr<node> next(cur->find(c));
      if (next) {
        cur = next;
      } else {
        while (cur != root && !cur->find(c)) cur = cur->suffix_link;
        if (cur->find(c)) cur = cur->find(c);
      }
      if (cur->accepting) fn(i+1-cur->length, cur->length);
      std::shared_ptr<node> out(cur->output_link);
      while (out) {
        fn(i+1-out->length, out->length);
        out = out->output_link;
      }
    }
  }

public:
  template <class ForwardIt>
  matching_automaton(ForwardIt first, ForwardIt last): root(new node) {
    build_nodes(first, last);
    build_links();
  }

  std::vector<size_t> longest_matches(const String& s) {
    std::vector<size_t> res(s.length());
    match_do(s, [&](size_t i, size_t j) { res[i] = std::max(res[i], j); });
    return res;
  }

  size_t longest_match(const String& s) {
    size_t res = 0;
    match_do(s, [&](size_t i, size_t j) { res = std::max(res, j); });
    return res;
  }
};

int testcase_ends() {
  char buf[4096];
  if (scanf("%s", buf) == EOF) return 1;
  std::string s = buf;
  scanf("%s", buf);
  std::string t = buf;

  if (s.length() > t.length()) std::swap(s, t);

  size_t n = s.length();
  size_t m = t.length();

  std::vector<std::string> ps;
  for (size_t i = 0; i < n; ++i)
    for (size_t j = 0; i+j <= n; ++j)
      ps.push_back(s.substr(i, j));

  matching_automaton<std::string> pma(ps.begin(), ps.end());
  printf("%zu\n", pma.longest_match(t));

  return 0;
}

int main() {
  while (!testcase_ends()) {}
}

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

string s, t;
int l[100][100];
int m;

void list(string s,string t){
    for(int i = 0; i < s.length(); i++){
        for(int j = 0; j < t.length(); j++){
            if(s[i]==t[j]){
                if(i==0||j==0){
                    l[i][j]=1;
                }else{
                    l[i][j]=l[i-1][j-1]+1;
                }
            }
        }
    }
}

int search(){
    m = 0;
    for(int i = 0; i < s.length(); i++){
        for(int j = 0; j < t.length(); j++){
            if(l[i][j] >= m){
                m=l[i][j];
                l[i][j]=0;
            }else{
                l[i][j]=0;
            }
        }
    }
    return m;
}

int main(){
    for(;;){
        m = 0;
        if(cin >> s >> t){
            m = 0;
            list(s,t);
            m = 0;
            cout << search() << endl;
        }else{
            break;
        }
        m = 0;
    }
    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;
			}
		}
		cout << ma << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

int main(){
  string s,t,u;
  while(cin>>s>>t){
    u=(s.size()>t.size())?t:s;
    if(s==u) s=t;
    int a=0,b=0;
    for(int i=0;i<s.size();++i){
      for(int j=0;j+i<s.size();++j){
	if(u[j]==s[i+j]) ++b;
	else{
	  a=max(a,b);b=0;
	}
      }
    }
    cout<<a<<endl;
  }
  return 0;
}

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

int DP[4001][4001];

int main() {
	string s, t;
	while(cin >> s >> t){
		memset(DP, 0, sizeof(DP));
		int n = s.size(), m = t.size(), ans = 0;
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < m; ++j){
				if(s[i] == t[j]){
					DP[i + 1][j + 1] = DP[i][j] + 1;
					ans = max(DP[i + 1][j + 1], ans);
				}
				else{
					DP[i + 1][j + 1] = 0;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}