# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
319916 | kostia244 | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 299 ms | 612 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
const int maxn = 3030;
int pref[maxn], suf[maxn];
array<int, 3> solve(string s, string t, int rev = 0) {
int n = s.size();
int ans = 0;
int P1 = 0, P2 = 0;
//cout << s << " " << t << endl;
for(int cut = 0; cut < n; cut++) {
memset(pref, 0, sizeof pref);
memset(suf, 0, sizeof suf);
vector<int> b{-1};
for(int i = 0, j = -1; cut+i < n; i++) {
while(j != -1 && s[cut+i] != s[cut+j]) j = b[j];
j++;
b.push_back(j);
}
for(int i = 0, j = 0; i < t.size(); i++) {
while(j != -1 && t[i] != s[cut+j]) j = b[j];
pref[i] = ++j;
if(j+1 == b.size()) j = b.back();
}
if(cut) {
cut--;
vector<int> b{-1};
for(int i = 0, j = -1; cut-i >= 0; i++) {
while(j != -1 && s[cut-i] != s[cut-j]) j = b[j];
j++;
b.push_back(j);
}
for(int i = t.size(), j = 0; i--;) {
while(j != -1 && t[i] != s[cut-j]) j = b[j];
suf[i] = ++j;
if(j+1 == b.size()) j = b.back();
}
cut++;
}
//cout << cut << ":\n";
//for(int i = 0; i < t.size(); i++) cout << pref[i] << " "; cout << endl;
//for(int i = 0; i < t.size(); i++) cout << suf[i] << " "; cout << endl;
for(int i = 0; i < t.size(); i++) {
if(ans < pref[i] + suf[i+1]) {
ans = pref[i]+suf[i+1];
P1 = cut-suf[i+1];
P2 = i+1-pref[i];
if(rev) P2 = t.size()-P2-ans;
//if(ans == 5) cout << cut << " " << i << " " << pref[i] << " " << suf[i+1] << endl;
}
}
}
return {ans, P1, P2};
}
int main() {
cin.tie(0)->sync_with_stdio(0);
string x, y;
cin >> x >> y;
auto ans = solve(x, y);
reverse(all(y));
ans = max(ans, solve(x, y, 1));
cout << ans[0] << '\n' << ans[1] << " " << ans[2] << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |