Submission #6574802


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;
  string x;
  while (x.size() < t.size() * 3) x += s;
  vector<bool> match(x.size());
  int u = 0;
  auto pre = kmp(t);
  rep(i, x.size()) {
    while (u != -1 && t[u] != x[i]) u = pre[u];
    u++;
    if (u == t.size()) {
      match[(i+1 - t.size()) % s.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 1473 Byte
Status WA
Exec Time 67 ms
Memory 36680 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 3
AC × 66
WA × 4
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 60 ms 36612 KB
b13 AC 67 ms 36680 KB
b14 AC 60 ms 36168 KB
b15 AC 30 ms 4892 KB
b16 AC 46 ms 32540 KB
b17 AC 21 ms 22600 KB
b18 AC 22 ms 22600 KB
b19 AC 51 ms 32540 KB
b20 AC 21 ms 22600 KB
b21 AC 25 ms 23112 KB
b22 AC 20 ms 23112 KB
b23 AC 44 ms 29896 KB
b24 AC 33 ms 25756 KB
b25 AC 20 ms 22600 KB
b26 WA 26 ms 23580 KB
b27 AC 21 ms 23112 KB
b28 AC 36 ms 27592 KB
b29 AC 22 ms 23624 KB
b30 AC 23 ms 23624 KB
b31 AC 20 ms 23112 KB
b32 AC 21 ms 22600 KB
b33 AC 20 ms 23052 KB
b34 AC 19 ms 22600 KB
b35 AC 22 ms 23112 KB
b36 AC 16 ms 20784 KB
b37 AC 20 ms 22600 KB
b38 AC 19 ms 22600 KB
b39 AC 20 ms 23112 KB
b40 AC 19 ms 22600 KB
b41 AC 19 ms 22600 KB
b42 AC 20 ms 23112 KB
b43 AC 24 ms 24264 KB
b44 AC 19 ms 22600 KB
b45 AC 24 ms 23880 KB
b46 AC 21 ms 23112 KB
b47 AC 21 ms 23112 KB
b48 AC 17 ms 21040 KB
b49 AC 19 ms 22600 KB
b50 AC 19 ms 22600 KB
b51 AC 21 ms 23496 KB
b52 AC 20 ms 22600 KB
b53 AC 21 ms 23240 KB
b54 AC 19 ms 22536 KB
b55 AC 19 ms 22600 KB
b56 WA 13 ms 19100 KB
b57 WA 13 ms 18972 KB
b58 WA 4 ms 4176 KB
b59 AC 1 ms 512 KB
b60 AC 5 ms 4700 KB
b61 AC 2 ms 1664 KB
b62 AC 3 ms 2044 KB
b63 AC 14 ms 18972 KB
b64 AC 20 ms 22600 KB
b65 AC 20 ms 22600 KB
b66 AC 20 ms 23112 KB
b67 AC 20 ms 23112 KB
b68 AC 19 ms 22600 KB
b69 AC 20 ms 22600 KB
b70 AC 21 ms 23112 KB