답안 #656149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
656149 2022-11-06T11:57:04 Z 600Mihnea Necklace (Subtask 1-3) (BOI19_necklace1) C++17
25 / 85
1500 ms 320 KB
bool home = 0;
bool verbose = 1;

#include <bits/stdc++.h>

using namespace std;

int get_longest(int last, int first, string& s, string& t) {
  int n = (int) s.size(), m = (int) t.size();
  if (!(0 <= last && last < n && 0 <= first && first < m)) {
    return 0;
  }
  int max_possible_len = min(last + 1, m - first);
  for (int len = max_possible_len; len >= 0; len--) {
    bool ok = 1;
    for (int i = 0; i < len; i++) {
      ok &= (s[last - len + 1 + i] == t[first + i]);
    }
    if (ok) {
      return len;
    }
  }
  return 0;
}

int main() {

  if (!home) {
    verbose = 0;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
  }
  if (home) {
    verbose = 1;
    freopen ("___input___.txt", "r", stdin);
  }

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

  int sol = -1, l, f, r;

  for (int rev_t = 0; rev_t <= 1; rev_t++) {
    int n = (int) s.size(), m = (int) t.size();
    /**

    AB
       BA
    **/
    for (int last_a = 0; last_a < n; last_a++) {
      for (int first_a = 0; first_a < m; first_a++) {
        int first_b = last_a + 1;
        int last_b = first_a - 1;
        int cur = get_longest(last_a, first_a, s, t) + get_longest(last_b, first_b, t, s);
        if (cur > sol) {
          sol = cur;
          l = last_a;
          f = first_a;
          r = rev_t;
        }
      }
    }
    reverse(t.begin(), t.end());
  }

  if (r) {
    reverse(t.begin(), t.end());
  }
  int rev = r;
  int last_a = l;
  int first_a = f;
  int first_b = last_a + 1;
  int last_b = first_a - 1;
  int len_a = get_longest(last_a, first_a, s, t);
  int len_b = get_longest(last_b, first_b, t, s);
  /// [last_a - len_a]
  /// [last_b - len_b]

  int jump_a = last_a - len_a + 1;
  int jump_b;

  /// [a, b]
  ///        [b, a]

  if (rev == 0) {
    jump_b = last_b - len_b + 1;
  } else {
    jump_b = (int) t.size() - (first_a + len_a);
  }
  cout << sol << "\n";
  cout << jump_a << " " << jump_b << "\n";
  return 0;
}

Compilation message

necklace.cpp: In function 'int main()':
necklace.cpp:36:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     freopen ("___input___.txt", "r", stdin);
      |     ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
necklace.cpp:67:3: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
   67 |   if (r) {
      |   ^~
necklace.cpp:89:40: warning: 'f' may be used uninitialized in this function [-Wmaybe-uninitialized]
   89 |     jump_b = (int) t.size() - (first_a + len_a);
      |                               ~~~~~~~~~^~~~~~~~
necklace.cpp:80:23: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
   80 |   int jump_a = last_a - len_a + 1;
      |                ~~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 312 KB Output is correct
2 Correct 14 ms 312 KB Output is correct
3 Correct 15 ms 312 KB Output is correct
4 Correct 18 ms 320 KB Output is correct
5 Correct 25 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 312 KB Output is correct
2 Correct 14 ms 312 KB Output is correct
3 Correct 15 ms 312 KB Output is correct
4 Correct 18 ms 320 KB Output is correct
5 Correct 25 ms 212 KB Output is correct
6 Execution timed out 1567 ms 212 KB Time limit exceeded
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 16 ms 312 KB Output is correct
2 Correct 14 ms 312 KB Output is correct
3 Correct 15 ms 312 KB Output is correct
4 Correct 18 ms 320 KB Output is correct
5 Correct 25 ms 212 KB Output is correct
6 Execution timed out 1567 ms 212 KB Time limit exceeded
7 Halted 0 ms 0 KB -