# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1075920 | vjudge1 | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 190 ms | 512 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>
using namespace std;
#define task "test"
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define vii vector <ii>
#define vi vector <int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
void solve();
int32_t main() {
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
solve();
}
const int N = 3005;
int lenS, lenT, kmpn[N], kmp[N], matchn[N], match[N], res, p1, p2; bool isFlip;
string s, t;
void _solve() {
FOR(j, 0, lenT) {
/// j, j+1
kmpn[j] = kmp[j+1] = 0;
FORD(i, j-1, 1) {
int tmp = kmpn[i+1];
while(tmp > 0 && t[i] != t[j-tmp]) tmp = kmpn[j-tmp+1];
kmpn[i] = (t[i] == t[j-tmp]) ? ++tmp : 0;
}
FOR(i, j+2, lenT) {
int tmp = kmp[i-1];
while(tmp > 0 && t[i] != t[j+1+tmp]) tmp = kmp[j+tmp];
kmp[i] = (t[i] == t[j+1+tmp]) ? ++tmp : 0;
}
match[0] = 0;
FOR(i, 1, lenS) {
int tmp = match[i-1];
while(tmp > 0 && s[i] != t[j+1+tmp]) tmp = kmp[j+tmp];
match[i] = (s[i] == t[j+1+tmp]) ? ++tmp : 0;
}
matchn[lenS+1] = 0;
FORD(i, lenS, 1) {
int tmp = matchn[i+1];
while(tmp > 0 && s[i] != t[j-tmp]) tmp = kmpn[j-tmp+1];
matchn[i] = (s[i] == t[j-tmp]) ? ++tmp : 0;
}
FOR(i, 0, lenS) {
if(match[i] + matchn[i+1] > res) {
res = match[i]+matchn[i+1];
p1 = i-match[i]+1;
if(isFlip) p1 = (lenS-p1+1)-res+1;
p2 = j-matchn[i+1]+1;
// cout << i << " " << j << " " << res << '\n';
}
}
}
}
void solve() {
cin >> s >> t;
lenS = s.length(); lenT = t.length();
s = " " + s; t = " " + t;
_solve();
reverse(s.begin()+1, s.end()); isFlip = 1;
// cout << s << '\n' << t << '\n';
_solve();
cout << res << '\n';
cout << p1-1 << " " << p2-1 << '\n';
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |