Submission #6583256


Source Code Expand

Copy
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)

using namespace std;
using ll = long long;

vector<int> kmp(string s) {
  int n = s.size();
  vector<int> p(n + 1);
  p[0] = -1;
  int j = -1;
  for (int i = 0; i < n; i++) {
    while (j != -1 && s[i] != s[j]) j = p[j];
    p[i + 1] = ++j;
  }
  return p;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  const int n = s.size();
  const int m = t.size();
  vector<bool> match(n);
  int u = 0;
  auto pre = kmp(t);
  rep(i, n + m) {
    while (u != -1 && t[u] != s[i % n]) u = pre[u];
    u++;
    if (u == m) {
      match[(i+1-m) % n] = true;
    }
  }
  vector<vector<int>> g(n);
  vector<int> deg(n);
  rep(i, n) {
    if (match[i]) {
      int j = (i + m) % n;
      g[i].push_back(j);
      deg[j]++;
    }
  }
  queue<int> q;
  rep(i, n) if (deg[i] == 0) q.push(i);
  vector<int> dp(n);
  int num = 0;
  while (!q.empty()) {
    num++;
    int u = q.front(); q.pop();
    for (int v : g[u]) {
      dp[v] = max(dp[v], dp[u] + 1);
      if (--deg[v] == 0) {
        q.push(v);
      }
    }
  }
  if (num != n) {
    cout << -1 << endl;
  } else {
    cout << *max_element(dp.begin(), dp.end()) << endl;
  }
}

Submission Info

Submission Time
Task F - Strings of Eternity
User pekempey
Language C++14 (GCC 5.4.1)
Score 600
Code Size 1358 Byte
Status AC
Exec Time 47 ms
Memory 34588 KB

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 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
b04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 1 ms 256 KB
b07 AC 1 ms 256 KB
b08 AC 1 ms 256 KB
b09 AC 1 ms 256 KB
b10 AC 1 ms 256 KB
b11 AC 1 ms 256 KB
b12 AC 47 ms 34588 KB
b13 AC 47 ms 34588 KB
b14 AC 47 ms 34588 KB
b15 AC 6 ms 3356 KB
b16 AC 42 ms 32028 KB
b17 AC 21 ms 20892 KB
b18 AC 22 ms 20892 KB
b19 AC 47 ms 32028 KB
b20 AC 23 ms 20892 KB
b21 AC 24 ms 20892 KB
b22 AC 20 ms 20892 KB
b23 AC 33 ms 27804 KB
b24 AC 31 ms 25244 KB
b25 AC 20 ms 20892 KB
b26 AC 26 ms 23068 KB
b27 AC 21 ms 20892 KB
b28 AC 28 ms 25500 KB
b29 AC 20 ms 21532 KB
b30 AC 21 ms 21532 KB
b31 AC 19 ms 20892 KB
b32 AC 22 ms 20892 KB
b33 AC 19 ms 20892 KB
b34 AC 20 ms 20892 KB
b35 AC 20 ms 20892 KB
b36 AC 17 ms 19740 KB
b37 AC 20 ms 20892 KB
b38 AC 20 ms 20892 KB
b39 AC 20 ms 20892 KB
b40 AC 20 ms 20892 KB
b41 AC 20 ms 20892 KB
b42 AC 20 ms 20892 KB
b43 AC 22 ms 22044 KB
b44 AC 19 ms 20892 KB
b45 AC 21 ms 21788 KB
b46 AC 19 ms 20892 KB
b47 AC 21 ms 21532 KB
b48 AC 18 ms 19996 KB
b49 AC 19 ms 20892 KB
b50 AC 19 ms 20892 KB
b51 AC 20 ms 21276 KB
b52 AC 19 ms 20892 KB
b53 AC 23 ms 21148 KB
b54 AC 20 ms 20892 KB
b55 AC 19 ms 21020 KB
b56 AC 15 ms 18588 KB
b57 AC 14 ms 18460 KB
b58 AC 5 ms 4048 KB
b59 AC 1 ms 384 KB
b60 AC 5 ms 4384 KB
b61 AC 2 ms 1536 KB
b62 AC 3 ms 1792 KB
b63 AC 14 ms 18460 KB
b64 AC 20 ms 21020 KB
b65 AC 20 ms 20892 KB
b66 AC 19 ms 20892 KB
b67 AC 19 ms 20892 KB
b68 AC 20 ms 20892 KB
b69 AC 20 ms 20892 KB
b70 AC 21 ms 20892 KB