Submission #834663

# Submission time Handle Problem Language Result Execution time Memory
834663 2023-08-22T16:43:45 Z mgl_diamond Necklace (Subtask 4) (BOI19_necklace4) C++17
0 / 15
261 ms 456 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
template<class T> using vec = vector<T>;
 
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define file "input"
 
void setIO() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  if (fopen(file".in", "r")) {
    freopen(file".in", "r", stdin);
    freopen(file".out", "w", stdout);
  }
}

const int N = 6006;
int n, m, kmp1[N], kmp2[N];
tuple<int, int, int> ans = {0, 0, 0};
string s, t, t2;

void calc(string s, int *kmp) {
  kmp[sz(s)] = 0;
  foru(i, 1, sz(s)-1) {
    int j = kmp[i-1];
    while (j > 0 && s[j] != s[i]) j = kmp[j-1];
    if (s[j] == s[i]) j += 1;
    kmp[i] = j;
  }
}

void solve(bool rev) {
  foru(i, 0, n) {
    string suf = s.substr(i, n-i), pre = s.substr(0, i);
    // if (rev) cout << pre << " " << suf << "\n";
    reverse(all(pre));
    // if (i == 2) cout << pre << " " << t2 << " | " << suf << " " << t << "\n";
    calc(suf + '#' + t, kmp1); calc(pre + '#' + t2, kmp2);
    // foru(j, n-i+1, n-i+m) cout << kmp1[j] << " ";
    // cout << "\n";
    // foru(j, i+1, i+m) cout << kmp2[j] << " ";
    // cout << "\n";
    // cout << sz(suf + "#" + t) << "\n";
    // cout << kmp1[n+1+1] << " ";
    // cout << kmp1[n-i+2] << " " << kmp2[i+2] << "\n";
    foru(j, 0, m) ans = max(ans, {kmp1[n-i+j]+kmp2[i+m-j], (rev ? n-i : i) - kmp1[n-i+j], j - kmp2[i+m-j]});
    // cout << get<0>(ans) << "\n";
  }
}

int main() {
  setIO();
  cin >> s >> t;
  n = sz(s); m = sz(t);
  t2 = t;
  reverse(all(t2));
  solve(0);
  reverse(all(s));
  solve(1);

  cout << get<0>(ans) << "\n" << get<1>(ans) << " " << get<2>(ans);
}

Compilation message

necklace.cpp: In function 'void setIO()':
necklace.cpp:20:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |     freopen(file".in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
necklace.cpp:21:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     freopen(file".out", "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 261 ms 456 KB Output isn't correct
2 Halted 0 ms 0 KB -