結果
| 問題 | No.2162 Copy and Paste 2 |
| コンテスト | |
| ユーザー |
vjudge1
|
| 提出日時 | 2026-10-08 20:48:49 |
| 言語 | C++23 (gcc 15.3.0 + boost 1.92.0 + ACL) |
| 結果 |
AC
不安定
|
| 実行時間 | 452 ms / 7,000 ms |
| + 128µs | |
| コード長 | 2,737 bytes |
| 記録 | |
| コンパイル時間 | 1,458 ms |
| コンパイル使用メモリ | 189,412 KB |
| 実行使用メモリ | 18,544 KB |
| 最終ジャッジ日時 | 2026-10-08 20:49:41 |
| 合計ジャッジ時間 | 9,259 ms |
|
ジャッジサーバーID (参考情報) |
judge4_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 26 |
ソースコード
#include <algorithm>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using u64 = unsigned long long;
constexpr int B1 = 1919, B2 = 1145, M1 = 998244853, M2 = 1e9 + 1111, N = 2e5;
inline int add(int x, int y, int mod) { return x + y >= mod ? x + y - mod : (x + y < 0 ? x + y + mod : x + y); }
inline int mul(int x, int y, int mod) { return x * (u64)y % mod; }
int base1[N + 1], base2[N + 1];
void prepare()
{
base1[0] = base2[0] = 1;
for (int i = 1; i <= N; ++i) {
base1[i] = mul(base1[i - 1], B1, M1);
base2[i] = mul(base2[i - 1], B2, M2);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
prepare();
string S;
cin >> S;
int n = S.size();
vector<int> hsh1(n + 1), hsh2(n + 1);
S = ' ' + S;
for (int i = 1; i <= n; ++i) {
hsh1[i] = add(hsh1[i - 1], mul(S[i], base1[i], M1), M1);
hsh2[i] = add(hsh2[i - 1], mul(S[i], base2[i], M2), M2);
}
auto get_hsh = [&](int l, int r) -> pair<int, int> { return { mul(add(hsh1[r], -hsh1[l - 1], M1), base1[N - r], M1), mul(add(hsh2[r], -hsh2[l - 1], M2), base2[N - r], M2) }; };
vector<int> lcp(n + 1); // lcp[i] := the LCP of S and S[i, n]
for (int i = 1; i <= n; ++i) {
int l = 1, r = n - i + 1;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (get_hsh(1, mid) == get_hsh(i, i + mid - 1))
l = mid;
else
r = mid - 1;
}
if (get_hsh(1, l) == get_hsh(i, i + l - 1))
lcp[i] = l;
}
vector<int> order(n - 1);
iota(order.begin(), order.end(), 1);
ranges::sort(order, { }, [&](int i) { return lcp[i + 1]; });
set<int> valid;
for (int i = 1; i < n; ++i)
valid.insert(valid.end(), i);
int cur = 0;
vector<int> g(n + 1);
for (int j = 1; j <= n; ++j) {
g[j] = max(g[j], g[j - 1]);
while (cur < n - 1 && lcp[order[cur] + 1] < j)
valid.erase(order[cur++]);
int f = g[j] - 1, i = j;
while (i <= n) {
auto itr = valid.lower_bound(i);
if (itr == valid.end())
break;
i = *itr;
if (i + j > n)
break;
f += j - 1;
g[i + j] = max(g[i + j], f);
i += j;
}
}
cout << n - g[n] << '\n';
return 0;
}
/*
f(i, j) -> f(i + 1, j)
f(i, j) - 1 -> f(i, i)
f(i, j) + j - 1 -> f(i + j, j) (lcp[i] >= j)
g(i) = max_{j <= i} f(i, j)
for each 1 <= j <= n:
f = g(j) - 1
for all lcp[i] >= j:
f += j - 1
g(i + j) = max(g(i + j), f)
*/
vjudge1