# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
524581 | fcw | Necklace (Subtask 4) (BOI19_necklace4) | C++17 | 89 ms | 65540 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 st first
#define nd second
using lint = int64_t;
constexpr int MOD = int(1e9) + 7;
constexpr int INF = 0x3f3f3f3f;
constexpr int NINF = 0xcfcfcfcf;
constexpr lint LINF = 0x3f3f3f3f3f3f3f3f;
const long double PI = acosl(-1.0);
// Returns -1 if a < b, 0 if a = b and 1 if a > b.
int cmp_double(double a, double b = 0, double eps = 1e-9) {
return a + eps > b ? b + eps > a ? 0 : 1 : -1;
}
using namespace std;
vector<int> Z(const string& S) {
vector<int> z(S.size());
int l = -1, r = -1;
for(int i = 1; i < int(S.size()); ++i) {
z[i] = i >= r ? 0 : min(r - i, z[i - l]);
while (i + z[i] < int(S.size()) && S[i + z[i]] == S[z[i]])
z[i]++;
if (i + z[i] > r) l = i, r = i + z[i];
}
return z;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
string s, t;
cin>>s>>t;
int n = (int)s.size(), m = (int)t.size();
string rt = t;
reverse(rt.begin(), rt.end());
vector<vector<int>>z(n), rz(n);
for(int i=0;i<n;i++){
z[i] = Z(s + t), rz[i] = Z(s + rt);
for(int j=0;j<m;j++){
z[i][n+j] = min(z[i][n+j], n-i);
rz[i][n+j] = min(rz[i][n+j], n-i);
}
rotate(s.begin(), s.begin() + 1, s.end());
}
vector<vector<int>>w(n, vector<int>(m));
vector<vector<int>>rw(n, vector<int>(m));
int ans = 0;
pair<int, int>ind = {-1, -1};
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int d = z[i][n+j], rd = rz[i][n+j];
if(d){
if(ans < d) ans = d, ind = {i, j};
int ni = i + d, nj = j + d;
if(ni < n) w[ni][j] = max(w[ni][j], d);
if(nj < m){
int nd = w[i][nj];
if(ans < d+nd) ans = d+nd, ind = {i-d, j};
}
}
if(rd){
if(ans < rd) ans = rd, ind = {i, m-(j+rd)};
int ni = i + rd, nj = j + rd;
if(ni < n) rw[ni][j] = max(rw[ni][j], rd);
if(nj < m){
int nd = rw[i][nj];
if(ans < rd+nd) ans = rd+nd, ind = {i-rd, m-(j+rd+nd)};
}
}
}
}
cout<<ans<<"\n";
cout<<ind.st<<" "<<ind.nd<<"\n";
return 0;
}
/*
[ ]Leu o problema certo???
[ ]Ver se precisa de long long
[ ]Viu o limite dos fors (é n? é m?)
[ ]Tamanho do vetor, será que é 2e5 em vez de 1e5??
[ ]Testar sample
[ ]Testar casos de borda
[ ]1LL no 1LL << i
[ ]Testar mod (é 1e9+7, mesmo?, será que o mod não ficou negativo?)
*/
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |