Submission #133363

# Submission time Handle Problem Language Result Execution time Memory
133363 2019-07-20T12:42:17 Z IOrtroiii Necklace (Subtask 1-3) (BOI19_necklace1) C++14
85 / 85
433 ms 504 KB
#include <bits/stdc++.h>

using namespace std;

vector<int> eval(string s, string t) {
   int n = s.size();
   int m = t.size();
   vector<int> link(n);
   if (s.empty()) {
      return vector<int>(m, 0);
   }
   link[0] = -1;
   int cur = -1;
   for (int i = 1; i < n; ++i) {
      while (~cur && s[cur + 1] != s[i]) {
         cur = link[cur];
      }
      if (s[cur + 1] == s[i]) {
         cur++;
      }
      link[i] = cur;
   }
   cur = -1;
   vector<int> ans(m);
   for (int i = 0; i < m; ++i) {
      while (~cur && s[cur + 1] != t[i]) {
         cur = link[cur];
      }
      if (s[cur + 1] == t[i]) {
         cur++;
      }
      ans[i] = cur + 1;
   }
   return ans;
}

void debug(vector<int> a, string s) {
   cout << s << " ";
   for (int v : a) cout << v << " ";
   cout << "\n";
}

int main() {
   ios_base::sync_with_stdio(false);
   string s, t;
   cin >> s >> t;
   int n = s.size();
   int m = t.size();
   auto solve = [&]() {
      array<int, 3> ans = {0, 0, 0};
      for (int i = 0; i <= m; ++i) {
         string l, r;
         for (int j = 0; j < m; ++j) {
            if (j < i) {
               l += t[j];
            } else {
               r += t[j];
            }
         }
         reverse(l.begin(), l.end());
         reverse(s.begin(), s.end());
         auto gol = eval(l, s);
         reverse(gol.begin(), gol.end());
         reverse(s.begin(), s.end());
         auto gor = eval(r, s);
         // debug(gol, "gol is:");
         // debug(gor, "gor is:");
         for (int j = 0; j <= n; ++j) {
            int lenL = (j == 0 ? 0 : gor[j - 1]);
            int lenR = (j == n ? 0 : gol[j]);
            array<int, 3> cur = {lenL + lenR, j - lenL, i - lenR};
            ans = max(ans, cur);
         }
      }
      return ans;
   };
   auto l = solve();
   reverse(t.begin(), t.end());
   auto r = solve();
   cout << max(l[0], r[0]) << "\n";
   if (l[0] < r[0]) {
      cout << r[1] << " " << m - r[2] - r[0] << "\n";
   } else {
      cout << l[1] << " " << l[2] << "\n";
   }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 11 ms 376 KB Output is correct
7 Correct 10 ms 376 KB Output is correct
8 Correct 10 ms 384 KB Output is correct
9 Correct 10 ms 376 KB Output is correct
10 Correct 9 ms 376 KB Output is correct
11 Correct 9 ms 376 KB Output is correct
12 Correct 9 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 11 ms 376 KB Output is correct
7 Correct 10 ms 376 KB Output is correct
8 Correct 10 ms 384 KB Output is correct
9 Correct 10 ms 376 KB Output is correct
10 Correct 9 ms 376 KB Output is correct
11 Correct 9 ms 376 KB Output is correct
12 Correct 9 ms 376 KB Output is correct
13 Correct 428 ms 376 KB Output is correct
14 Correct 381 ms 476 KB Output is correct
15 Correct 433 ms 376 KB Output is correct
16 Correct 408 ms 504 KB Output is correct
17 Correct 335 ms 504 KB Output is correct
18 Correct 350 ms 504 KB Output is correct
19 Correct 364 ms 504 KB Output is correct
20 Correct 383 ms 504 KB Output is correct
21 Correct 391 ms 440 KB Output is correct