結果

問題 No.2162 Copy and Paste 2
ユーザー miscalc
提出日時 2021-08-28 02:30:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,156 ms / 7,000 ms
コード長 2,532 bytes
コンパイル時間 2,744 ms
コンパイル使用メモリ 211,500 KB
最終ジャッジ日時 2025-01-24 03:50:02
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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;
}

// https://qiita.com/keymoon/items/11fac5627672a6d6a9f6
class rollinghash
{
public:
  const ll MOD = (1LL << 61) - 1;
  const ll MASK61 = MOD;
  const ll MASK31 = (1LL << 31) - 1;
  const ll MASK30 = (1LL << 30) - 1;
  const ll POSITIVISER = 3 * MOD;
  ll Base;

  ll mul(ll a, ll b)
  {
    ll au = a >> 31; // (a / MASK31)
    ll ad = a & MASK31; // (a % MASK31)
    ll bu = b >> 31;
    ll bd = b & MASK31;
    ll m = au * bd + ad * bu;
    ll mu = m >> 30;
    ll md = m & MASK30;

    return au * bu * 2 + mu + (md << 31) + ad * bd;
  }

  ll calcmod(ll x)
  {
    ll xu = x >> 61;
    ll xd = x & MASK61;

    ll ret = xu + xd;
    if (ret >= MOD)
      ret -= MOD;

    return ret;
  }

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

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

    srand((unsigned)time(NULL));
    Base = rand() % 1024 + 256;

    powmem.resize(N + 1);
    powmem.at(0) = 1;
    for (ll i = 0; i < N; i++)
    {
      powmem.at(i + 1) = calcmod(mul(powmem.at(i), Base));
    }
    
    hash.resize(N + 1);
    hash.at(0) = 1;
    for (ll i = 0; i < N; i++)
    {
      hash.at(i + 1) = calcmod(mul(hash.at(i), Base) + S.at(i));
    }
  }

  ll sub(ll pos, ll len)
  {
    return calcmod(hash.at(pos + len) + POSITIVISER - mul(hash.at(pos), powmem.at(len)));
  }
};

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.sub(0, mid) == rh.sub(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