# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1076641 | vjudge1 | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 212 ms | 596 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 ll long long
#define ld long double
#define eb emplace_back
#define task ""
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
#define fi first
#define se second
#define pii pair <int, int>
#define tii tuple <int, int, int>
#define all(s) s.begin(), s.end()
using namespace std;
const int nmax = 3002;
string s, t;
int n, m;
tii ans = {0, 0, 0};
vector <int> kmp(string s)
{
vector <int> res(s.size());
int k = 0;
for(int i = 1; i < s.size(); ++i)
{
while(k && s[i] != s[k])
k = res[k - 1];
res[i] = (s[i] == s[k] ? ++k : 0);
}
return res;
}
int revpos(int p, int len)
{
return len - p - 1;
}
void solve(bool rev)
{
for (int i = 0; i < n; ++i)
{
string s1 = s.substr(0, i);
string s2 = s.substr(i, n - i);
reverse(all(s1));
string t1 = t, t2 = t;
reverse(all(t2));
vector <int> p1 = kmp(s1 + '!' + t2), p2 = kmp(s2 + '!' + t1);
for (int j = 0; j < m; ++j)
{
int x = p2[j + s2.size() + 1];
int y = p1[revpos(j + 1, m) + s1.size() + 1];
ans = max(ans, make_tuple(x + y, i - y, (!rev ? j - x + 1 : revpos(j + y, m))));
}
}
}
int main()
{
if (ifstream(task".inp")) nx
fast
cin >> s >> t;
n = s.size(), m = t.size();
solve(0);
reverse(all(t));
solve(1);
cout << get<0>(ans) << '\n' << get<1>(ans) << ' ' << get<2>(ans);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |