Submission #6575793


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() * 2) x += s;
  while (x.size() < s.size() * 2) 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 600
Code Size 1516 Byte
Status AC
Exec Time 66 ms
Memory 36168 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 57 ms 35612 KB
b13 AC 65 ms 36168 KB
b14 AC 57 ms 35612 KB
b15 AC 20 ms 4252 KB
b16 AC 60 ms 33072 KB
b17 AC 21 ms 22044 KB
b18 AC 22 ms 22044 KB
b19 AC 66 ms 33072 KB
b20 AC 22 ms 22044 KB
b21 AC 26 ms 22600 KB
b22 AC 21 ms 22600 KB
b23 AC 43 ms 29384 KB
b24 AC 41 ms 26288 KB
b25 AC 20 ms 22044 KB
b26 AC 34 ms 24112 KB
b27 AC 23 ms 22600 KB
b28 AC 36 ms 27080 KB
b29 AC 24 ms 23112 KB
b30 AC 24 ms 23112 KB
b31 AC 22 ms 22600 KB
b32 AC 21 ms 22044 KB
b33 AC 21 ms 22044 KB
b34 AC 21 ms 22044 KB
b35 AC 23 ms 22600 KB
b36 AC 19 ms 20784 KB
b37 AC 20 ms 22044 KB
b38 AC 20 ms 22044 KB
b39 AC 21 ms 22600 KB
b40 AC 21 ms 22044 KB
b41 AC 20 ms 22044 KB
b42 AC 22 ms 22600 KB
b43 AC 27 ms 23752 KB
b44 AC 20 ms 22044 KB
b45 AC 25 ms 23368 KB
b46 AC 22 ms 22600 KB
b47 AC 22 ms 22556 KB
b48 AC 20 ms 21040 KB
b49 AC 19 ms 22044 KB
b50 AC 20 ms 22044 KB
b51 AC 22 ms 22856 KB
b52 AC 19 ms 22044 KB
b53 AC 22 ms 22728 KB
b54 AC 20 ms 22044 KB
b55 AC 19 ms 22044 KB
b56 AC 17 ms 19632 KB
b57 AC 17 ms 19508 KB
b58 AC 5 ms 4176 KB
b59 AC 2 ms 512 KB
b60 AC 5 ms 4512 KB
b61 AC 3 ms 1664 KB
b62 AC 3 ms 1920 KB
b63 AC 17 ms 19504 KB
b64 AC 21 ms 22044 KB
b65 AC 22 ms 22044 KB
b66 AC 22 ms 22600 KB
b67 AC 22 ms 22600 KB
b68 AC 21 ms 22044 KB
b69 AC 20 ms 22044 KB
b70 AC 22 ms 22600 KB