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: Accepted)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_STRING_SIZE = 100;

ll base = 4049;

vector<ll> MODS({2147483647, 6700417});

vector<vector<ll>> invs(MODS.size()), pows(MODS.size());

ll powm(ll e, ll x, ll m) {
  ll r = 1;
  while (x) {
    if (x % 2) {
      r = (r * e) % m;
    }
    e = (e * e) % m;
    x >>= 1;
  }
  return r;
}

void RHinit() {
  int i = 0;
  for (auto &p : MODS) {
    invs[i].resize(MAX_STRING_SIZE + 1);
    invs[i][0] = 1;
    ll inv = powm(base, p - 2, p);
    for (int j = 1; j <= MAX_STRING_SIZE; j++) {
      invs[i][j] = (invs[i][j - 1] * inv) % p;
    }
    pows[i].resize(MAX_STRING_SIZE + 1);
    pows[i][0] = 1;
    for (int j = 1; j <= MAX_STRING_SIZE; j++) {
      pows[i][j] = (pows[i][j - 1] * base) % p;
    }
    i++;
  }
}

vector<ll> h(vector<int> &s) {
  vector<ll> ret(MODS.size());
  int n = s.size();
  for (int i = 0; i < MODS.size(); i++) {
    ll r = 0;
    for (int j = 0; j < n; j++) {
      r = (r + pows[i][j] * s[j]) % MODS[i];
    }
    ret[i] = r;
  }
  return ret;
}

struct RH {
  int n;
  vector<int> s;
  vector<vector<ll>> hs;

  RH(vector<int> &s) : s(s) {
    if (pows[0].empty()) {
      RHinit();
    }
    n = s.size();
    hs.resize(MODS.size());
    for (int i = 0; i < MODS.size(); i++) {
      hs[i].resize(n + 1);
      hs[i][0] = 0;
      for (int j = 0; j < n; j++) {
        hs[i][j + 1] = (hs[i][j] + pows[i][j] * s[j]) % MODS[i];
      }
    }
  }

  vector<ll> substr(int l, int r) {
    vector<ll> ret(MODS.size());
    for (int i = 0; i < MODS.size(); i++) {
      ret[i] = (hs[i][r] - hs[i][l] + MODS[i]) % MODS[i];
      ret[i] *= invs[i][l];
      ret[i] %= MODS[i];
    }
    return ret;
  }
};
ll hh(vector<ll> &ha) { return ha[0] * (1LL << 32) + ha[1]; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  string s1, s2;
  cin >> s1 >> s2;
  RHinit();
  int ans = 0;
  int n = s1.size(), m = s2.size();
  for (int k = 1; k <= min(n, m); k++) {
    unordered_set<ll> sz;
    vector<int> v(26);
    for (int i = 0; i < k; i++) {
      v[s1[i] - 'a']++;
    }
    auto hs = h(v);
    sz.insert(hh(hs));
    for (int i = k; i < n; i++) {
      hs[0] += MODS[0] - pows[0][s1[i - k] - 'a'];
      hs[0] %= MODS[0];
      hs[1] += MODS[1] - pows[1][s1[i - k] - 'a'];
      hs[1] %= MODS[1];
      hs[0] += pows[0][s1[i] - 'a'];
      hs[0] %= MODS[0];
      hs[1] += pows[1][s1[i] - 'a'];
      hs[1] %= MODS[1];
      sz.insert(hh(hs));
    }
    v.clear();
    v.resize(26);
    for (int i = 0; i < k; i++) {
      v[s2[i] - 'a']++;
    }
    hs = h(v);
    auto it = sz.find(hh(hs));
    if (it != sz.end()) {
      ans = max(ans, k);
    }
    for (int i = k; i < m; i++) {
      hs[0] += MODS[0] - pows[0][s2[i - k] - 'a'];
      hs[0] %= MODS[0];
      hs[1] += MODS[1] - pows[1][s2[i - k] - 'a'];
      hs[1] %= MODS[1];
      hs[0] += pows[0][s2[i] - 'a'];
      hs[0] %= MODS[0];
      hs[1] += pows[1][s2[i] - 'a'];
      hs[1] %= MODS[1];
      it = sz.find(hh(hs));
      if (it != sz.end()) {
        ans = max(ans, k);
      }
    }
  }
  cout << ans << endl;

  return 0;
}