Submission #74947720


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
#include <atcoder/all>
using namespace atcoder;
// using mint = modint998244353;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

template <typename T> using v = vector<T>;
template <typename T> using vv = vector<vector<T>>;
template <typename T> using vvv = vector<vector<vector<T>>>;

using ll = long long;       using ld = long double;     using vs = v<string>;
using vi = v<int>;          using vll = v<ll>;          using vld = v<ld>;
using vvi = vv<int>;        using vvll = vv<ll>;        using vvld = vv<ld>;
using vvvi = vvv<int>;      using vvvll = vvv<ll>;      using vvvld = vvv<ld>;
using pii = pair<int, int>; using pll = pair<ll, ll>;   using pld = pair<ld, ld>;
using vpii = v<pii>;        using vpll = v<pll>;        using vpld = v<pld>;
using mll = map<ll, ll>;    using umll = unordered_map<ll, ll>;
using pq_gr = priority_queue<ll, vi, greater<ll>>;

// ======================================================================================

/*
    問題 : T * i は S * ? の 部分文字列か?

    T == S * j のとき : inf

    S == Tr + T * j + Tl のとき : inf

    => 問題 : T * i は S * inf の 部分文字列として何連続で出現するか?

    ACL の Z algorithm、
    入力を T + '#' + S + S ( |S| >= |T| ) ってすれば S[i .. i + |T| - 1] == T かどうか取れるのか

    DAG はスニペットにしている
*/

// >---------------- DAG [Begin] ---------------->

struct TopologicalSort {
    int n;
    vvi g;
    vi indeg;

    TopologicalSort(int n) : n(n), g(n), indeg(n) {}

    void add_edge(int a, int b) {
        g[a].push_back(b);
        indeg[b]++;
    }

    // false のとき閉路あり
    // true のとき、order がトポロジカルソート順
    bool sort(vi &order) {
        // 辞書順最小にしたいなら
        // priority_queue<int, vi, greater<int>> q;
        queue<int> q;
        vi deg = indeg;

        for (int i = 0; i < n; i++) {
            if (deg[i] == 0) q.push(i);
        }

        while (!q.empty()) {
            int v = q.front(); q.pop();
            order.push_back(v);

            for (int to : g[v]) {
                deg[to]--;
                if (deg[to] == 0) q.push(to);
            }
        }

        return (int)order.size() == n;
    }
};

// DAG 上の最長パスと親の記録
// dp[t] = 任意始点から t までの最長距離
// usage : auto [dp, par] = longest_path_dag(ts.g, order);
pair<vi, vi> longest_path_dag(const vvi &g, const vi &order) {
    int n = g.size();
    vi dp(n), par(n, -1);

    for (int v : order) {
        for (int to : g[v]) {
            if (dp[to] < dp[v] + 1) {
                dp[to] = dp[v] + 1;
                par[to] = v;
            }
        }
    }

    return {dp, par};
}

// t までの経路復元
vi restore_path(int t, const vi &par) {
    vi path;
    while (t != -1) {
        path.push_back(t);
        t = par[t];
    }
    reverse(path.begin(), path.end());
    return path;
}

// <---------------- DAG [ End ] ----------------<

void solve() {
    string S, T;
    cin >> S >> T;
    string s = S;
    while (S.size() < T.size()) S += s;

    int N = S.size(), M = T.size();

    string C = T + '#' + S + S;
    vi za = z_algorithm(C);

    TopologicalSort ts(N);
    int fix = M + 1;
    for (int i = 0; i < N; i++) {
        if (za[i + fix] == M) {
            ts.add_edge(i, (i + M) % N);
        }
    }

    vi order;
    if (!ts.sort(order)) {
        cout << -1 << '\n';
        return;
    }

    auto [dp, _] = longest_path_dag(ts.g, order);
    cout << *max_element(all(dp)) << '\n';
}

// ======================================================================================

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // int T; cin >> T;
    // while (T--) solve();

    solve();
}

Submission Info

Submission Time
Task F - Strings of Eternity
User integlim
Language C++23 (GCC 15.2.0)
Score 600
Code Size 4039 Byte
Status AC
Exec Time 71 ms
Memory 79992 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 70
Set Name Test Cases
Sample a01, a02, a03
All a01, a02, a03, b04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55, b56, b57, b58, b59, b60, b61, b62, b63, b64, b65, b66, b67, b68, b69, b70
Case Name Status Exec Time Memory
a01 AC 1 ms 3500 KiB
a02 AC 1 ms 3448 KiB
a03 AC 1 ms 3500 KiB
b04 AC 1 ms 3500 KiB
b05 AC 1 ms 3564 KiB
b06 AC 1 ms 3564 KiB
b07 AC 1 ms 3572 KiB
b08 AC 1 ms 3524 KiB
b09 AC 1 ms 3572 KiB
b10 AC 1 ms 3536 KiB
b11 AC 1 ms 3484 KiB
b12 AC 40 ms 43344 KiB
b13 AC 71 ms 79992 KiB
b14 AC 38 ms 43352 KiB
b15 AC 39 ms 42848 KiB
b16 AC 31 ms 40508 KiB
b17 AC 30 ms 37712 KiB
b18 AC 30 ms 37708 KiB
b19 AC 40 ms 48280 KiB
b20 AC 30 ms 37644 KiB
b21 AC 54 ms 68664 KiB
b22 AC 53 ms 68672 KiB
b23 AC 58 ms 70444 KiB
b24 AC 33 ms 41660 KiB
b25 AC 29 ms 37568 KiB
b26 AC 29 ms 39316 KiB
b27 AC 54 ms 68528 KiB
b28 AC 54 ms 68592 KiB
b29 AC 47 ms 61688 KiB
b30 AC 55 ms 69780 KiB
b31 AC 54 ms 68652 KiB
b32 AC 28 ms 37680 KiB
b33 AC 25 ms 33800 KiB
b34 AC 29 ms 37644 KiB
b35 AC 54 ms 68460 KiB
b36 AC 27 ms 36172 KiB
b37 AC 30 ms 37536 KiB
b38 AC 30 ms 37572 KiB
b39 AC 54 ms 68668 KiB
b40 AC 30 ms 37684 KiB
b41 AC 29 ms 37708 KiB
b42 AC 54 ms 68728 KiB
b43 AC 49 ms 62760 KiB
b44 AC 29 ms 37676 KiB
b45 AC 56 ms 70320 KiB
b46 AC 54 ms 68624 KiB
b47 AC 27 ms 34244 KiB
b48 AC 26 ms 36568 KiB
b49 AC 29 ms 37592 KiB
b50 AC 30 ms 37708 KiB
b51 AC 46 ms 61488 KiB
b52 AC 30 ms 37636 KiB
b53 AC 54 ms 68860 KiB
b54 AC 30 ms 37532 KiB
b55 AC 26 ms 33936 KiB
b56 AC 25 ms 34732 KiB
b57 AC 24 ms 34732 KiB
b58 AC 6 ms 9860 KiB
b59 AC 1 ms 4000 KiB
b60 AC 7 ms 10256 KiB
b61 AC 3 ms 5612 KiB
b62 AC 5 ms 8028 KiB
b63 AC 25 ms 34588 KiB
b64 AC 28 ms 37536 KiB
b65 AC 30 ms 37716 KiB
b66 AC 55 ms 68584 KiB
b67 AC 54 ms 68480 KiB
b68 AC 30 ms 37708 KiB
b69 AC 29 ms 37568 KiB
b70 AC 53 ms 68624 KiB