Submission #6573253


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;
  vector<bool> match(s.size() * 2);
  int u = 0;
  auto pre = kmp(t);
  rep(i, s.size() * 2) {
    while (u != -1 && t[u] != s[i % s.size()]) u = pre[u];
    u++;
    if (u == t.size()) {
      match[i+1 - t.size()] = true;
    }
  }
  vector<vector<int>> g(s.size());
  vector<int> deg(s.size());
  rep(i, s.size()) {
    if (match[i]) {
      int j = (i + t.size()) % s.size();
      g[i].push_back(j);
      deg[j]++;
    }
  }
  queue<int> q;
  rep(i, s.size()) {
    if (deg[i] == 0) {
      q.push(i);
    }
  }
  vector<int> dp(s.size());
  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 != s.size()) {
    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 0
Code Size 1423 Byte
Status WA
Exec Time 70 ms
Memory 34588 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 2
WA × 1
AC × 63
WA × 7
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 WA 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 62 ms 34588 KB
b13 AC 62 ms 34588 KB
b14 AC 62 ms 34588 KB
b15 WA 4 ms 3356 KB
b16 AC 65 ms 32156 KB
b17 AC 32 ms 21020 KB
b18 AC 32 ms 21020 KB
b19 AC 70 ms 32156 KB
b20 AC 31 ms 21020 KB
b21 AC 38 ms 21020 KB
b22 AC 26 ms 21020 KB
b23 WA 46 ms 27804 KB
b24 AC 49 ms 25372 KB
b25 AC 30 ms 21020 KB
b26 AC 42 ms 23068 KB
b27 AC 29 ms 21020 KB
b28 WA 40 ms 25500 KB
b29 WA 28 ms 21532 KB
b30 AC 28 ms 21532 KB
b31 AC 26 ms 21060 KB
b32 AC 27 ms 21020 KB
b33 AC 26 ms 21020 KB
b34 AC 26 ms 21020 KB
b35 AC 26 ms 21020 KB
b36 AC 24 ms 19740 KB
b37 AC 28 ms 21020 KB
b38 AC 28 ms 21020 KB
b39 AC 26 ms 21020 KB
b40 AC 26 ms 21020 KB
b41 AC 27 ms 21020 KB
b42 AC 26 ms 21020 KB
b43 WA 29 ms 22172 KB
b44 AC 26 ms 21020 KB
b45 AC 28 ms 21916 KB
b46 AC 26 ms 21020 KB
b47 AC 27 ms 21660 KB
b48 AC 26 ms 20124 KB
b49 AC 26 ms 21148 KB
b50 AC 26 ms 21020 KB
b51 WA 28 ms 21404 KB
b52 AC 26 ms 20956 KB
b53 AC 27 ms 21148 KB
b54 AC 26 ms 21020 KB
b55 AC 25 ms 21148 KB
b56 AC 24 ms 18588 KB
b57 AC 23 ms 18588 KB
b58 AC 7 ms 4048 KB
b59 AC 1 ms 384 KB
b60 AC 6 ms 4384 KB
b61 AC 3 ms 1536 KB
b62 AC 3 ms 1792 KB
b63 AC 23 ms 18588 KB
b64 AC 26 ms 21020 KB
b65 AC 27 ms 21020 KB
b66 AC 26 ms 21020 KB
b67 AC 26 ms 21020 KB
b68 AC 27 ms 21020 KB
b69 AC 27 ms 21020 KB
b70 AC 27 ms 21020 KB