Submission #834690

# Submission time Handle Problem Language Result Execution time Memory
834690 2023-08-22T17:10:57 Z mgl_diamond Necklace (Subtask 1-3) (BOI19_necklace1) C++17
85 / 85
270 ms 480 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;
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;
  }
}

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

int main() {
  setIO();
  cin >> s >> t;
  n = sz(s); m = sz(t);
  t2 = t;
  reverse(all(t2));
  ans = solve(0);
  // cout << get<0>(ans) << "\n";
  reverse(all(s));
  ans = max(ans, 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 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 340 KB Output is correct
8 Correct 5 ms 320 KB Output is correct
9 Correct 5 ms 320 KB Output is correct
10 Correct 4 ms 340 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 5 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 328 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 328 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 5 ms 376 KB Output is correct
7 Correct 5 ms 340 KB Output is correct
8 Correct 5 ms 320 KB Output is correct
9 Correct 5 ms 320 KB Output is correct
10 Correct 4 ms 340 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 5 ms 340 KB Output is correct
13 Correct 258 ms 452 KB Output is correct
14 Correct 224 ms 456 KB Output is correct
15 Correct 270 ms 440 KB Output is correct
16 Correct 241 ms 444 KB Output is correct
17 Correct 195 ms 444 KB Output is correct
18 Correct 203 ms 460 KB Output is correct
19 Correct 210 ms 480 KB Output is correct
20 Correct 232 ms 456 KB Output is correct
21 Correct 238 ms 452 KB Output is correct