Luzhiled's Library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub ei1333/library

:heavy_check_mark: test/verify/yosupo-longest-common-substring.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/longest_common_substring

#include "../../template/template.hpp"

#include "../../string/longest-common-substring.hpp"

int main() {
  string S, T;
  cin >> S >> T;
  auto [a, b, c, d] = longest_common_substring(S, T);
  cout << a << " " << b << " " << c << " " << d << endl;
}
#line 1 "test/verify/yosupo-longest-common-substring.test.cpp"
// competitive-verifier: PROBLEM https://judge.yosupo.jp/problem/longest_common_substring

#line 1 "template/template.hpp"
#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
#endif

using namespace std;

using int64 = long long;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < (int)v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

template <typename T = int64>
vector<T> make_v(size_t a) {
  return vector<T>(a);
}

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t) fill_v(e, v);
}

template <typename F>
struct FixPoint : F {
  explicit FixPoint(F &&f) : F(std::forward<F>(f)) {}

  template <typename... Args>
  decltype(auto) operator()(Args &&...args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
inline decltype(auto) MFP(F &&f) {
  return FixPoint<F>{std::forward<F>(f)};
}
#line 4 "test/verify/yosupo-longest-common-substring.test.cpp"

#line 1 "string/suffix-array.hpp"
/**
 * @brief Suffix Array(接尾辞配列)
 */
template <typename T>
struct SuffixArray : vector<int> {
 private:
  vector<int> sa_is(const vector<int> &s) const {
    const int n = (int)s.size();
    vector<int> ret(n);

    vector<int> is_s(n), is_lms(n);
    int m = 0;
    for (int i = n - 2; i >= 0; i--) {
      is_s[i] = (s[i] > s[i + 1]) or (s[i] == s[i + 1] and is_s[i + 1]);
      m += (is_lms[i + 1] = is_s[i] and not is_s[i + 1]);
    }

    auto induced_sort = [&](const vector<int> &lms) {
      int upper = *max_element(s.begin(), s.end());
      vector<int> l(upper + 2), r(upper + 2);
      for (auto &&v : s) {
        ++l[v + 1];
        ++r[v];
      }
      partial_sum(l.begin(), l.end(), l.begin());
      partial_sum(r.begin(), r.end(), r.begin());
      fill(ret.begin(), ret.end(), -1);
      for (int i = (int)lms.size() - 1; i >= 0; i--) {
        ret[--r[s[lms[i]]]] = lms[i];
      }
      for (auto &&v : ret) {
        if (v >= 1 and is_s[v - 1]) ret[l[s[v - 1]]++] = v - 1;
      }
      fill(r.begin(), r.end(), 0);
      for (auto &&v : s) ++r[v];
      partial_sum(r.begin(), r.end(), r.begin());
      for (int k = (int)ret.size() - 1, i = ret[k]; k >= 1; i = ret[--k]) {
        if (i >= 1 and not is_s[i - 1]) {
          ret[--r[s[i - 1]]] = i - 1;
        }
      }
    };

    vector<int> lms;
    lms.reserve(m);
    for (int i = 1; i < n; i++) {
      if (is_lms[i]) lms.push_back(i);
    }

    induced_sort(lms);

    vector<int> new_lms;
    new_lms.reserve(m);
    for (int i = 0; i < n; i++) {
      if (not is_s[ret[i]] and ret[i] > 0 and is_s[ret[i] - 1]) {
        new_lms.push_back(ret[i]);
      }
    }

    auto is_same = [&](int a, int b) {
      if (s[a++] != s[b++]) return false;
      for (;; ++a, ++b) {
        if (s[a] != s[b]) return false;
        if (is_lms[a] or is_lms[b]) return is_lms[a] and is_lms[b];
      }
    };

    int rank = 0;
    ret[n - 1] = 0;
    for (int i = 1; i < m; i++) {
      if (not is_same(new_lms[i - 1], new_lms[i])) ++rank;
      ret[new_lms[i]] = rank;
    }

    if (rank + 1 < m) {
      vector<int> new_s(m);
      for (int i = 0; i < m; i++) {
        new_s[i] = ret[lms[i]];
      }
      auto lms_sa = sa_is(new_s);
      for (int i = 0; i < m; i++) {
        new_lms[i] = lms[lms_sa[i]];
      }
    }

    induced_sort(new_lms);

    return ret;
  }

 public:
  T vs;

  explicit SuffixArray(const T &vs, bool compress = false) : vs(vs) {
    vector<int> new_vs(vs.size() + 1);
    if (compress) {
      T xs = vs;
      sort(xs.begin(), xs.end());
      xs.erase(unique(xs.begin(), xs.end()), xs.end());
      for (int i = 0; i < (int)vs.size(); i++) {
        new_vs[i] =
            std::lower_bound(xs.begin(), xs.end(), vs[i]) - xs.begin() + 1;
      }
    } else {
      auto d = *min_element(vs.begin(), vs.end());
      for (int i = 0; i < (int)vs.size(); i++) {
        new_vs[i] = vs[i] - d + 1;
      }
    }
    auto ret = sa_is(new_vs);
    assign(ret.begin(), ret.end());
  }

  void output() const {
    for (int i = 0; i < size(); i++) {
      cout << i << ":[" << (*this)[i] << "]";
      for (int j = (*this)[i]; j < (int)vs.size(); j++) cout << " " << vs[j];
      cout << "\n";
    }
  }

  bool lt_substr(const string &t, int si = 0, int ti = 0) {
    int sn = (int)vs.size(), tn = (int)t.size();
    while (si < sn && ti < tn) {
      if (vs[si] < t[ti]) return true;
      if (vs[si] > t[ti]) return false;
      ++si, ++ti;
    }
    return si >= sn && ti < tn;
  }

  // t <= s[i,N) なる最小の i を返す O(|t| log |s|)
  int lower_bound(const T &t) {
    int ng = 0, ok = (int)size();
    while (ok - ng > 1) {
      int mid = (ok + ng) / 2;
      if (lt_substr(t, at(mid)))
        ng = mid;
      else
        ok = mid;
    }
    return ok;
  }

  // O(|t| log |s|)
  pair<int, int> equal_range(T &t) {
    int low = lower_bound(t);
    int ng = low - 1, ok = (int)size();
    t.back()++;
    while (ok - ng > 1) {
      int mid = (ok + ng) / 2;
      if (lt_substr(t, at(mid)))
        ng = mid;
      else
        ok = mid;
    }
    t.back()--;
    return {low, ok};
  }
};

template <>
void SuffixArray<string>::output() const {
  for (int i = 0; i < (int)size(); i++) {
    cout << i << ":[" << (*this)[i] << "] " << vs.substr((*this)[i]) << "\n";
  }
}
#line 2 "string/lcp-array.hpp"

/**
 * @brief LCP Array
 */
template <typename T>
vector<int> lcp_array(const SuffixArray<T> &sa) {
  int n = (int)sa.size() - 1;
  vector<int> lcp(n + 1), rank(n + 1);
  for (int i = 0; i <= n; i++) {
    rank[sa[i]] = i;
  }
  int h = 0;
  for (int i = 0; i <= n; i++) {
    if (rank[i] < n) {
      int j = sa[rank[i] + 1];
      for (; j + h < n && i + h < n; h++) {
        if (sa.vs[j + h] != sa.vs[i + h]) break;
      }
      lcp[rank[i] + 1] = h;
      if (h > 0) h--;
    }
  }
  return lcp;
}
#line 2 "string/longest-common-substring.hpp"

template <typename S>
tuple<int, int, int, int> longest_common_substring(const S& s, const S& t,
                                                   bool compress = false) {
  int n = (int)s.size(), m = (int)t.size();
  auto dummy =
      max(*max_element(s.begin(), s.end()), *max_element(t.begin(), t.end()));
  S st;
  st.reserve(n + 1 + m);
  for (auto& x : s) st.push_back(x);
  st.push_back(dummy + 1);
  for (auto& x : t) st.push_back(x);
  SuffixArray sa(st, compress);
  auto lcp = lcp_array(sa);
  tuple<int, int, int, int> res{0, 0, 0, 0};
  int ret = 0;
  for (int i = 0; i < (int)st.size() - 1; i++) {
    int x = sa[i], y = sa[i + 1];
    if (x > y) swap(x, y);
    if (x < n and n < y and ret < lcp[i + 1]) {
      y -= n + 1;
      ret = lcp[i + 1];
      res = {x, x + ret, y, y + ret};
    }
  }
  return res;
}
#line 6 "test/verify/yosupo-longest-common-substring.test.cpp"

int main() {
  string S, T;
  cin >> S >> T;
  auto [a, b, c, d] = longest_common_substring(S, T);
  cout << a << " " << b << " " << c << " " << d << endl;
}

Test cases

Env Name Status Elapsed Memory
g++ all_same_00 :heavy_check_mark: AC 36 ms 22 MB
g++ all_same_01 :heavy_check_mark: AC 34 ms 22 MB
g++ all_same_02 :heavy_check_mark: AC 35 ms 22 MB
g++ all_same_03 :heavy_check_mark: AC 35 ms 22 MB
g++ example_00 :heavy_check_mark: AC 6 ms 3 MB
g++ example_01 :heavy_check_mark: AC 5 ms 3 MB
g++ example_02 :heavy_check_mark: AC 5 ms 3 MB
g++ example_03 :heavy_check_mark: AC 5 ms 3 MB
g++ max_large_ans_00 :heavy_check_mark: AC 93 ms 30 MB
g++ max_large_ans_01 :heavy_check_mark: AC 103 ms 31 MB
g++ max_large_ans_02 :heavy_check_mark: AC 96 ms 33 MB
g++ max_large_ans_03 :heavy_check_mark: AC 107 ms 33 MB
g++ max_random_00 :heavy_check_mark: AC 102 ms 30 MB
g++ max_random_01 :heavy_check_mark: AC 103 ms 31 MB
g++ max_random_02 :heavy_check_mark: AC 104 ms 33 MB
g++ max_random_03 :heavy_check_mark: AC 102 ms 32 MB
g++ random_00 :heavy_check_mark: AC 83 ms 25 MB
g++ random_01 :heavy_check_mark: AC 91 ms 28 MB
g++ random_02 :heavy_check_mark: AC 49 ms 16 MB
g++ random_03 :heavy_check_mark: AC 50 ms 16 MB
g++ small_00 :heavy_check_mark: AC 5 ms 3 MB
g++ small_01 :heavy_check_mark: AC 5 ms 3 MB
g++ small_02 :heavy_check_mark: AC 5 ms 3 MB
g++ small_03 :heavy_check_mark: AC 5 ms 3 MB
g++ small_04 :heavy_check_mark: AC 5 ms 3 MB
g++ small_05 :heavy_check_mark: AC 5 ms 3 MB
g++ small_06 :heavy_check_mark: AC 5 ms 3 MB
g++ small_07 :heavy_check_mark: AC 5 ms 3 MB
clang++ all_same_00 :heavy_check_mark: AC 32 ms 22 MB
clang++ all_same_01 :heavy_check_mark: AC 31 ms 22 MB
clang++ all_same_02 :heavy_check_mark: AC 31 ms 22 MB
clang++ all_same_03 :heavy_check_mark: AC 31 ms 22 MB
clang++ example_00 :heavy_check_mark: AC 5 ms 3 MB
clang++ example_01 :heavy_check_mark: AC 5 ms 3 MB
clang++ example_02 :heavy_check_mark: AC 5 ms 3 MB
clang++ example_03 :heavy_check_mark: AC 5 ms 3 MB
clang++ max_large_ans_00 :heavy_check_mark: AC 88 ms 30 MB
clang++ max_large_ans_01 :heavy_check_mark: AC 96 ms 31 MB
clang++ max_large_ans_02 :heavy_check_mark: AC 92 ms 33 MB
clang++ max_large_ans_03 :heavy_check_mark: AC 100 ms 33 MB
clang++ max_random_00 :heavy_check_mark: AC 96 ms 30 MB
clang++ max_random_01 :heavy_check_mark: AC 97 ms 31 MB
clang++ max_random_02 :heavy_check_mark: AC 99 ms 33 MB
clang++ max_random_03 :heavy_check_mark: AC 95 ms 32 MB
clang++ random_00 :heavy_check_mark: AC 78 ms 25 MB
clang++ random_01 :heavy_check_mark: AC 85 ms 28 MB
clang++ random_02 :heavy_check_mark: AC 46 ms 16 MB
clang++ random_03 :heavy_check_mark: AC 47 ms 16 MB
clang++ small_00 :heavy_check_mark: AC 5 ms 3 MB
clang++ small_01 :heavy_check_mark: AC 5 ms 3 MB
clang++ small_02 :heavy_check_mark: AC 5 ms 3 MB
clang++ small_03 :heavy_check_mark: AC 5 ms 3 MB
clang++ small_04 :heavy_check_mark: AC 5 ms 3 MB
clang++ small_05 :heavy_check_mark: AC 5 ms 3 MB
clang++ small_06 :heavy_check_mark: AC 5 ms 3 MB
clang++ small_07 :heavy_check_mark: AC 5 ms 3 MB
Back to top page