답안 #651119

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
651119 2022-10-17T09:00:06 Z lto5 Necklace (Subtask 1-3) (BOI19_necklace1) C++14
85 / 85
494 ms 35772 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 3005;

short f[N][N], g[N][N];
int pi[2*N];
int ans;
int start_a = -1;
int start_b = -1;

void solve (string s, string t, bool reversed = false) {
  int n = s.size();
  int m = t.size();

  s = ' ' + s;
  t = ' ' + t;
  string cur = "";

  auto reset = [&]() {
    int lim = n + m + 5;
    for (int i = 0; i <= lim; i++)
      pi[i] = 0;
  };

  auto add = [&] (int i) -> int {
    int j = pi[i - 1];
    while (j > 0 && cur[j] != cur[i]) {
      j = pi[j - 1];
    }
    if (cur[i] == cur[j])
      pi[i] = j + 1;

    return pi[i];
  };

  for (int cut_b = 1; cut_b <= m; cut_b++) {
    reset();
    cur = t.substr(cut_b);
    cur = cur + '.';

    for (int i = 1; i < cur.size(); i++) {
      add(i);
    }

    for (int cut_a = 1; cut_a <= n; cut_a++) {
      cur.push_back(s[cut_a]);
      f[cut_a][cut_b] = add((int)cur.size() - 1);
    }
  }

  for (int cut_a = 1; cut_a <= n; cut_a++) {
    reset();
    cur = s.substr(cut_a);
    cur = cur + '.';

    for (int i = 1; i < cur.size(); i++) {
      add(i);
    }

    for (int cut_b = 1; cut_b <= m; cut_b++) {
      cur.push_back(t[cut_b]);
      g[cut_a][cut_b] = add((int)cur.size() - 1);
    }
  }

  for (int cut_a = 1; cut_a <= n; cut_a++)
    for (int cut_b = 1; cut_b <= m; cut_b++) {
      int cur = f[cut_a][cut_b] + g[cut_a + 1][cut_b - 1];
      if (ans < cur) {
        ans = cur;
        start_a = cut_a - f[cut_a][cut_b] + 1;
        start_b = cut_b - 1 - g[cut_a + 1][cut_b - 1] + 1;
        if (reversed) {
          start_b += cur - 1;
          start_b = m - start_b + 1;
        }
      }
    }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  string s, t;
  cin >> s >> t;

  solve(s, t);
  reverse(t.begin(), t.end());
  solve(s, t, 1);

  cout << ans << "\n";
  cout << start_a-1 << " " << start_b-1;

  return 0;
}

Compilation message

necklace.cpp: In function 'void solve(std::string, std::string, bool)':
necklace.cpp:42:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |     for (int i = 1; i < cur.size(); i++) {
      |                     ~~^~~~~~~~~~~~
necklace.cpp:57:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     for (int i = 1; i < cur.size(); i++) {
      |                     ~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 11 ms 4180 KB Output is correct
7 Correct 9 ms 4180 KB Output is correct
8 Correct 11 ms 4052 KB Output is correct
9 Correct 11 ms 3924 KB Output is correct
10 Correct 10 ms 4192 KB Output is correct
11 Correct 8 ms 4052 KB Output is correct
12 Correct 10 ms 3964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1108 KB Output is correct
2 Correct 2 ms 1108 KB Output is correct
3 Correct 2 ms 980 KB Output is correct
4 Correct 2 ms 1108 KB Output is correct
5 Correct 1 ms 1108 KB Output is correct
6 Correct 11 ms 4180 KB Output is correct
7 Correct 9 ms 4180 KB Output is correct
8 Correct 11 ms 4052 KB Output is correct
9 Correct 11 ms 3924 KB Output is correct
10 Correct 10 ms 4192 KB Output is correct
11 Correct 8 ms 4052 KB Output is correct
12 Correct 10 ms 3964 KB Output is correct
13 Correct 461 ms 35724 KB Output is correct
14 Correct 429 ms 35772 KB Output is correct
15 Correct 494 ms 34512 KB Output is correct
16 Correct 481 ms 35548 KB Output is correct
17 Correct 338 ms 35104 KB Output is correct
18 Correct 355 ms 35640 KB Output is correct
19 Correct 391 ms 35576 KB Output is correct
20 Correct 438 ms 35100 KB Output is correct
21 Correct 465 ms 35268 KB Output is correct