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 <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>
#include <string>
#include <algorithm>

#define MAXLEN 4001

using namespace std;

string str[2];
int len[2],lng,sht;

int main(void)
{
	while(true)
	{
		cin >> str[0];
		if(cin.eof())
		{
			exit(0);
		}
		cin >> str[1];
		len[0] = str[0].length();
		len[1] = str[1].length();
		if(len[0] < len[1])
		{
			lng = 1;
			sht = 0;
		}
		else
		{
			lng = 0;
			sht = 1;
		}
		int count_len_max = 0;
		for(int offset = 0;offset < len[lng];offset++)
		{
			int count_len = 0;
			for(int count = 0;count < len[sht];count++)
			{
				if(str[lng][(count + offset)%len[lng]] == str[sht][count])
				{
					count_len++;
				}
				else
				{
					count_len = 0;
				}
				count_len_max = max(count_len_max,count_len);
			}
		}
		cout << count_len_max << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define NCLR(a) memset((a), -1 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
typedef complex<double> P;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 1000000007;
static const int INF = 1 << 29;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int dp[ 4 ][ 32768 ];

int main() {
	int ans = 0;
	string s;
	cin >> s;
	int i = 0;
	while ( s.size() != i ) {
		int _j = 0, _o = 0, _i = 0;
		while ( s.size() != i && s[ i ] == 'J' ) {
			_j++;
			i++;
		}
		while ( s.size() != i && s[ i ] == 'O' ) {
			_o++;
			i++;
		}
		while ( s.size() != i && s[ i ] == 'I' ) {
			_i++;
			i++;
		}
		if ( _o <= _j && _o <= _i ) {
			ans = max( _o, ans );
		}
	}
	printf( "%d\n", ans );
	return 0;
}