# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
857123 | hgglmao | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 220 ms | 852 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 TASK "necklace"
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<int, ll> ill;
typedef pair<ll, int> lli;
typedef pair<ll, ll> pll;
const int MAX = 6001;
const int MOD = 1e9 + 7;
string a, b;
int na, nb;
int rs, ra, rb;
int k1[MAX], k2[MAX];
void KYS(string s, int *kys)
{
kys[0] = 0;
for (int i = 1; i < s.size(); ++i)
{
int j = kys[i - 1];
while (j > 0 && s[i] != s[j])
j = kys[j - 1];
if (s[i] == s[j])
kys[i] = j + 1;
else
kys[i] = 0;
}
}
void SOLVE(bool revb)
{
for (int i = 0; i < na; ++i)
{
string al = a.substr(0, i), ar = a.substr(i, na - i);
string bl = b, br = b;
reverse(al.begin(), al.end());
reverse(br.begin(), br.end());
KYS(al + "!" + bl, k1);
KYS(ar + "!" + br, k2);
for (int j = 1; j <= nb; ++j)
if (rs < k1[i + j] + k2[na + nb - i - j])
{
rs = k1[i + j] + k2[na + nb - i - j];
ra = i - k1[i + j];
if (revb)
rb = nb - j - k2[na + nb - i - j];
else
rb = j - k1[i + j];
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if (fopen(TASK ".inp", "r"))
{
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
cin >> a >> b;
na = a.size(), nb = b.size();
SOLVE(0);
reverse(b.begin(), b.end());
SOLVE(1);
cout << rs << '\n'
<< ra << ' ' << rb;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |