Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Problem D
Hidden Anagrams

An anagram is a word or a phrase that is formed by rearranging the letters of another. For instance, by rearranging the letters of "William Shakespeare," we can have its anagrams "I am a weakish speller," "I'll make a wise phrase," and so on. Note that when $A$ is an anagram of $B$, $B$ is an anagram of $A$.

In the above examples, differences in letter cases are ignored, and word spaces and punctuation symbols are freely inserted and/or removed. These rules are common but not applied here; only exact matching of the letters is considered.

For two strings $s_1$ and $s_2$ of letters, if a substring $s'_1$ of $s_1$ is an anagram of a substring $s'_2$ of $s_2$, we call $s'_1$ a hidden anagram of the two strings, $s_1$ and $s_2$. Of course, $s'_2$ is also a hidden anagram of them.

Your task is to write a program that, for given two strings, computes the length of the longest hidden anagrams of them.

Suppose, for instance, that "anagram" and "grandmother" are given. Their substrings "nagr" and "gran" are hidden anagrams since by moving letters you can have one from the other. They are the longest since any substrings of "grandmother" of lengths five or more must contain "d" or "o" that "anagram" does not. In this case, therefore, the length of the longest hidden anagrams is four. Note that a substring must be a sequence of letters occurring consecutively in the original string and so "nagrm" and "granm" are not hidden anagrams.

Input

The input consists of a single test case in two lines.

$s_1$

$s_2$

$s_1$ and $s_2$ are strings consisting of lowercase letters (a through z) and their lengths are between 1 and 4000, inclusive.

Output

Output the length of the longest hidden anagrams of $s_1$ and $s_2$. If there are no hidden anagrams, print a zero.

Sample Input 1

anagram
grandmother

Sample Output 1

4

Sample Input 2

williamshakespeare
iamaweakishspeller

Sample Output 2

18

Sample Input 3

aaaaaaaabbbbbbbb
xxxxxabababxxxxxabab

Sample Output 3

6

Sample Input 4

abababacdcdcd
efefefghghghghgh

Sample Output 4

0

Solution in C++14 (judge verdict: Memory Limit Exceeded)

/*input
anagram
grandmother
*/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define rep(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
#define mset(x, v) memset((x), (v), sizeof (x) )
#define buff() ios::sync_with_stdio(0);cin.tie(0)
#define sz(x) ( (int) (x).size() )

using vi  = vector<int>;
using ll  = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vvi = vector<vi>;

namespace std { 
    template<> struct hash<pair<int, int> > { 
        size_t operator() (const pair<int, int>& key) const {
            return hash<int>()(key.first) + hash<int>()(key.second);
        }
    };
}

constexpr int mod = 1000000007;
constexpr int inf = 0x3f3f3f3f;

inline ll powmod(ll a, ll b) {
    ll res = 1;
    a %= mod;
    assert(b >= 0);
    for(;b;b>>=1) {
        if(b&1) res = (res * a) % mod;
        a = (a*a) % mod;
    }
    return res;
}

int mul(int a, int b, int p) {
    ll x = a, y = b, z = p;
    return (x * y) % z;
}

array<int, 2> primes = {mod, mod + 16};
array<int, 4001> alphaSize[2];
array<int, 2> val = {0, 0};

struct rollingHash {
    int alpha;
    rollingHash(int alphaSz = 26) {
        alpha = alphaSz;
        rep(i,0,2) {
            alphaSize[i][0] = 1;
            rep(j,1,alpha) alphaSize[i][j] = mul(alphaSize[i][j-1], alpha, primes[i]);
        }
    }
    void addC(char& c) {
        int idx = c - 'a';
        rep(i,0,2) val[i] = ( val[i] + alphaSize[i][idx] ) % primes[i];
    }
    void clear() {
        val[0] = 0ll;
        val[1] = 0ll;
    }
};

string a, b;
unordered_set<pii> memo;
rollingHash rk(256);

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> a >> b;
    int lenA = sz(a), lenB = sz(b);
    rep(st, 0, lenA) {
        rk.clear();
        rep(x, st, lenA) {
            rk.addC(a[x]);
            memo.insert(make_pair(val[0], val[1]));
        }
    }
    int ans = 0;
    rep(st, 0, lenB) {
        rk.clear();
        rep(x, st, lenB) {
            rk.addC(b[x]);
            if(memo.find(make_pair(val[0], val[1]) ) != memo.end() ) {
                ans = max(ans, x - st + 1);
            }
        }
    }
    cout << ans << endl;
    return 0;
}