Submission #557678

#TimeUsernameProblemLanguageResultExecution timeMemory
557678Soumya1Necklace (Subtask 4) (BOI19_necklace4)C++17
15 / 15
447 ms588 KiB
#include <bits/stdc++.h> #ifdef __LOCAL__ #include <debug_local.h> #endif using namespace std; vector<int> prefix_function(string &s) { int n = s.size(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[j] != s[i]) j = pi[j - 1]; if (s[j] == s[i]) j++; pi[i] = j; } return pi; } vector<int> suffix_function(string s) { // :P there is no such thing. I just named it (actually same as prefix function) int n = s.size(); vector<int> pi(n); pi[n - 1] = n - 1; for (int i = n - 2; i >= 0; i--) { int j = pi[i + 1]; while (j < n - 1 && s[j] != s[i]) j = pi[j + 1]; if (s[j] == s[i]) j--; pi[i] = j; } for (int &i : pi) { i = n - 1 - i; } return pi; } tuple<int, int, int> solve(string s, string t, bool f = false) { int n = s.size(), m = t.size(); tuple<int, int, int> ret = {0, 0, 0}; for (int i = 0; i < n; i++) { string s1 = s.substr(0, i); string s2 = s.substr(i, n - i); string ss1 = s2 + '#' + t; string ss2 = t + '#' + s1; auto pi = prefix_function(ss1); auto si = suffix_function(ss2); int a[m], b[m]; memset(a, 0, sizeof a); memset(b, 0, sizeof b); for (int j = 0; j < m; j++) { a[j] = pi[j + n - i + 1]; b[j] = si[j]; } for (int j = 0; j < m; j++) { ret = max(ret, {a[j], i, j - a[j] + 1}); ret = max(ret, {b[j], i - b[j], j}); if (j >= 1) { ret = max(ret, {a[j - 1] + b[j], i - b[j], j - a[j - 1]}); } } } if (f) { auto &[l, i, j] = ret; i = n - i - l; } return ret; } void testCase() { string s, t; cin >> s >> t; auto a = solve(s, t); reverse(s.begin(), s.end()); auto b = solve(s, t, true); a = max(a, b); auto [l, i, j] = a; cout << l << "\n"; cout << i << " " << j << "\n"; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); testCase(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...