結果

問題 No.2162 Copy and Paste 2
ユーザー miscalcmiscalc
提出日時 2021-08-28 12:15:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,238 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 216,664 KB
実行使用メモリ 50,704 KB
最終ジャッジ日時 2024-04-24 13:00:15
合計ジャッジ時間 22,502 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 120 ms
40,592 KB
testcase_07 AC 126 ms
40,300 KB
testcase_08 AC 479 ms
48,144 KB
testcase_09 AC 333 ms
40,656 KB
testcase_10 AC 359 ms
40,084 KB
testcase_11 AC 515 ms
45,072 KB
testcase_12 AC 541 ms
42,508 KB
testcase_13 AC 861 ms
48,276 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 1,087 ms
48,396 KB
testcase_18 AC 1,343 ms
50,448 KB
testcase_19 AC 1,169 ms
48,916 KB
testcase_20 AC 1,092 ms
48,584 KB
testcase_21 AC 615 ms
48,332 KB
testcase_22 AC 588 ms
48,400 KB
testcase_23 AC 80 ms
38,164 KB
testcase_24 AC 1,508 ms
50,704 KB
testcase_25 AC 1,589 ms
50,588 KB
testcase_26 AC 1,508 ms
50,576 KB
testcase_27 AC 1,539 ms
50,580 KB
testcase_28 AC 1,223 ms
44,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#include <atcoder/lazysegtree>
using namespace atcoder;

int mymax(int a, int b)
{
  return max(a, b);
}

int e()
{
  return 0;
}

class rollinghash
{
public:
  ll K = 4;
  vector<ll> Base;

  vector<vector<ll>> powmem;
  vector<vector<ll>> hash;

  rollinghash(string S)
  {
    ll N = S.size();

    srand((unsigned)time(NULL));
    Base.resize(K);
    for (ll i = 0; i < K; i++)
    {
      Base.at(i) = rand() % 1024 + 256;
    }

    powmem.resize(K);
    hash.resize(K);
    for (ll i = 0; i < K; i++)
    {
      powmem.at(i).resize(N + 1);
      powmem.at(i).at(0) = 1;
      hash.at(i).resize(N + 1);
      hash.at(i).at(0) = 1;
      for (ll j = 0; j < N; j++)
      {
        powmem.at(i).at(j + 1) = powmem.at(i).at(j) * Base.at(i);
        hash.at(i).at(j + 1) = hash.at(i).at(j) * Base.at(i) + S.at(j);
      }
    }
  }

  ll sub(ll pos, ll len, ll i)
  {
    return hash.at(i).at(pos + len) - hash.at(i).at(pos) * powmem.at(i).at(len);
  }

  bool match(ll pos1, ll pos2, ll len)
  {
    for (ll i = 0; i < K; i++)
    {
      if (sub(pos1, len, i) != sub(pos2, len, i))
        return false;
    }
    return true;
  }
};

int main()
{
  string S;
  cin >> S;
  int N = S.size();
  S += string(N + 10, '.');

  rollinghash rh(S);
  vector<int> Z(N);
  for (int i = 0; i < N; i++)
  {
    int ok = 0, ng = N + 1;
    while (abs(ok - ng) > 1)
    {
      int mid = (ok + ng) / 2;
      if (rh.match(0, i, mid))
        ok = mid;
      else
        ng = mid;
    }
    Z.at(i) = ok;
  }

  for (int i = 0; i < N; i++)
  {
    Z.at(i) = min(Z.at(i), i);
  }

  vector<vector<int>> invZ(N);
  set<int> st;
  for (int i = 0; i < N; i++)
  {
    if (Z.at(i) >= 2)
    {
      invZ.at(Z.at(i)).push_back(i);
      st.emplace(i);
    }
  }
  st.emplace(N + 1);

  lazy_segtree<int, mymax, e, int, mymax, mymax, e> dp(N + 1);
  for (int j = 2; j < N; j++)
  {
    int cnt = dp.get(j) + j - 2;
    int i = *st.begin() + j;
    while (i < N + 1)
    {
      dp.apply(i, N + 1, cnt);

      cnt += j - 1;
      i = *st.lower_bound(i) + j;
    }

    for (auto ii : invZ.at(j))
    {
      st.erase(ii);
    }
  }

  int ans = N - dp.get(N);
  cout << ans << endl;
}
0