Submission #1155256

#TimeUsernameProblemLanguageResultExecution timeMemory
1155256nathan4690Necklace (Subtask 4) (BOI19_necklace4)C++20
15 / 15
848 ms2824 KiB
#include <bits/stdc++.h> #define ll long long #define ld long double #define f1(i,n) for(int i=1;i<=n;i++) #define __file_name "" using namespace std; const ll maxn = 1e6+5, inf=1e18; struct Result{ int len, i, j; Result(int len=0, int i=0, int j=0): len(len), i(i), j(j){}; bool operator<(const Result &rhs) const{ return len < rhs.len; } }; int n, m; string s, t; vector<int> outs[3005]; priority_queue<int, vector<int>, greater<int>> pq; bool del[3005]; Result solve(){ Result ans; for(int pos=0;pos<m;pos++){ string pref = t.substr(0, pos+1), suff = t.substr(pos+1, m); string p = suff + '#' + s; int sz = p.size(); vector<int> z(sz, 0); for (int i = 1, l = 0, r = 0; i < sz; ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < sz && p[z[i]] == p[i + z[i]]) ++z[i]; if (i + z[i] - 1 > r) { l = i; r = i + z[i] - 1; } } reverse(z.begin(), z.end()); z.resize(n); reverse(z.begin(), z.end()); reverse(pref.begin(), pref.end()); p = s; reverse(p.begin(), p.end()); p = pref + '#' + p; sz = p.size(); vector<int> pi(sz, 0); for (int i = 1; i < sz; i++) { int j = pi[i - 1]; while (j > 0 && p[i] != p[j]) j = pi[j - 1]; if (p[i] == p[j]) j++; pi[i] = j; } reverse(pi.begin(), pi.end()); pi.resize(n); for(int i=0;i<=n;i++) { outs[i].clear(); del[i] = 0; } while(!pq.empty()) pq.pop(); for(int i=0;i<n;i++){ for(int item: outs[i]){ del[item] = true; } while(!pq.empty()){ int j = pq.top(); if(del[j]) pq.pop(); else break; } if(!pq.empty()){ int j = pq.top(); ans = max(ans, Result(pi[i] + i - j, j, pos - pi[i] + 1)); }else { ans = max(ans, Result(pi[i], i, pos - pi[i] + 1)); } if(z[i]){ pq.push(i); outs[i + z[i] + 1].push_back(i); } } // if(pos == 92){ // cout << pref << '\n' << suff << '\n'; // cout << s << ' ' << t << ' ' << pos << '\n'; // for(int item: pi) cout << item << ' '; cout << '\n'; // for(int item: z) cout << item << ' '; cout << '\n'; // } } return ans; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); if(fopen(__file_name ".inp", "r")){ freopen(__file_name ".inp","r",stdin); freopen(__file_name ".out","w",stdout); } cin >> s >> t; n = s.size(); m = t.size(); Result res1 = solve(); reverse(t.begin(), t.end()); Result res2 = solve(); res2.j = m - 1 - (res2.j + res2.len - 1); res1 = max(res1, res2); cout << res1.len << '\n' << res1.i << ' ' << res1.j << '\n'; return 0; }

Compilation message (stderr)

necklace.cpp: In function 'int main()':
necklace.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
necklace.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...