#include <bits/stdc++.h>
using namespace std;
using ld = long double;
const ld EPS = 1e-10;
int main() {
string s, t; cin >> s >> t;
mt19937_64 rng(52);
vector<complex<ld>> rnd(256);
for (int i = 0; i < 256; ++i) {
ld real = 1.0L * rng() / rng.max();
ld imag = 1.0L * rng() / rng.max();
rnd[i] = {real, imag};
}
vector<pair<ld, int>> vs, vt;
auto check = [&](int len) {
complex<ld> eps = polar(1.0L, 2.0L * M_PI / len);
pair<int, int> ans = {-1, -1};
for (int rev = 0; rev < 2; ++rev) {
vs.clear(); vt.clear();
for (int it = 0; it < 2; ++it) {
complex<ld> h = 0;
for (int i = 0; i < (int)s.size(); ++i) {
h = (h + rnd[s[i]]) * eps;
if (i >= len - 1) {
vs.emplace_back(norm(h), i - len + 1);
h -= rnd[s[i - len + 1]];
}
}
swap(s, t); swap(vs, vt);
}
sort(vs.begin(), vs.end());
sort(vt.begin(), vt.end());
for (int i = 0, j = 0; i < (int)vs.size(); ++i) {
while (j < vt.size() && vs[i].first + EPS > vt[j].first) {
if (abs(vt[j].first - vs[i].first) < EPS) {
ans.first = vs[i].second;
ans.second = vt[j].second;
if (rev) ans.first = s.size() - ans.first - len;
}
++j;
}
}
reverse(s.begin(), s.end());
}
return ans;
};
int ans = 0, ps = -1, pt = -1;
int bound = 1;
while (check(bound).first >= 0) bound *= 2;
for (int i = bound; i >= bound/2; --i) {
auto [a, b] = check(i);
if (a >= 0) {
ans = i, ps = a, pt = b;
break;
}
}
cout << ans << endl << ps << " " << pt << endl;
return 0;
}
Compilation message
necklace.cpp: In lambda function:
necklace.cpp:38:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long double, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | while (j < vt.size() && vs[i].first + EPS > vt[j].first) {
| ~~^~~~~~~~~~~
necklace.cpp: In function 'int main()':
necklace.cpp:56:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
56 | auto [a, b] = check(i);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
4 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
340 KB |
Output is correct |
11 |
Correct |
6 ms |
340 KB |
Output is correct |
12 |
Correct |
4 ms |
340 KB |
Output is correct |
13 |
Correct |
185 ms |
636 KB |
Output is correct |
14 |
Correct |
176 ms |
624 KB |
Output is correct |
15 |
Correct |
114 ms |
620 KB |
Output is correct |
16 |
Correct |
130 ms |
624 KB |
Output is correct |
17 |
Correct |
50 ms |
624 KB |
Output is correct |
18 |
Correct |
47 ms |
624 KB |
Output is correct |
19 |
Correct |
350 ms |
624 KB |
Output is correct |
20 |
Correct |
247 ms |
624 KB |
Output is correct |
21 |
Correct |
359 ms |
624 KB |
Output is correct |