이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii array<int, 2>
int power(int a, ll b, int mod) {
int ret = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1)
ret = 1ll * ret * a % mod;
return ret;
}
const int N = 3e3 + 4;
const int P = 31, M = 1e9 + 9;
const int inv = power(P, M - 2, M);
int n[2], pw[N];
string s[2];
pii check(int sz) {
vector<pii> hashes[2];
for (int i = 0; i < 2; ++i) {
for (int j = 0; j + sz - 1 < n[i]; ++j) {
int h = 0;
for (int k = j; k < j + sz; ++k) {
h = (h + 1ll * s[i][k] * pw[k - j]) % M;
}
hashes[i].pb({h, j});
for (int k = j + 1; k < j + sz; ++k) {
h = (h - s[i][k - 1] + M) % M;
h = 1ll * h * inv % M;
h = (h + 1ll * s[i][k - 1] * pw[sz - 1]) % M;
hashes[i].pb({h, j});
}
}
}
for (int i = 0; i < 2; ++i) {
reverse(s[i].begin(), s[i].end());
}
for (int i = 0; i < 2; ++i) {
for (int j = 0; j + sz - 1 < n[i]; ++j) {
int h = 0;
for (int k = j; k < j + sz; ++k) {
h = (h + 1ll * s[i][k] * pw[k - j]) % M;
}
int t = j + sz - 1;
hashes[i].pb({h, n[i] - t - 1});
for (int k = j + 1; k < j + sz; ++k) {
h = (h - s[i][k - 1] + M) % M;
h = 1ll * h * inv % M;
h = (h + 1ll * s[i][k - 1] * pw[sz - 1]) % M;
hashes[i].pb({h, n[i] - t - 1});
}
}
}
for (int i = 0; i < 2; ++i) {
reverse(s[i].begin(), s[i].end());
sort(hashes[i].begin(), hashes[i].end());
}
int pt = 0;
for (auto [h, idx] : hashes[0]) {
while (pt > hashes[1][pt][0]) {
++pt;
if (pt == hashes[1].size()) {
break;
}
}
if (pt != hashes[1].size()) {
if (hashes[1][pt][0] == h) {
return {idx, hashes[1][pt][1]};
}
}
}
return {-1, -1};
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
pw[0] = 1;
for (int i = 1; i < N; ++i) {
pw[i] = 1ll * pw[i - 1] * P % M;
}
cin >> s[0] >> s[1];
n[0] = s[0].size();
n[1] = s[1].size();
int l = 0, r = min(n[0], n[1]) + 1, m;
while (r - l > 1) {
m = (l + r) / 2;
pii res = check(m);
pii neg = {-1, -1};
if (res != neg) {
l = m;
}
else {
r = m;
}
}
cout << l << '\n';
pii ans = check(l);
cout << ans[0] << ' ' << ans[1];
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
necklace.cpp: In function 'std::array<int, 2> check(int)':
necklace.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | if (pt == hashes[1].size()) {
| ~~~^~~~~~~~~~~~~~~~~~~
necklace.cpp:76:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | if (pt != hashes[1].size()) {
| ~~~^~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |