結果

問題 No.1695 Mirror Mirror
ユーザー 👑 emthrmemthrm
提出日時 2021-10-02 00:02:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,612 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 202,976 KB
実行使用メモリ 9,332 KB
最終ジャッジ日時 2023-09-26 23:41:25
合計ジャッジ時間 18,629 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 13 ms
9,196 KB
testcase_22 AC 12 ms
9,088 KB
testcase_23 TLE -
testcase_24 AC 131 ms
8,972 KB
testcase_25 AC 9 ms
7,072 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 8 ms
7,020 KB
testcase_31 AC 11 ms
8,884 KB
testcase_32 AC 18 ms
4,668 KB
testcase_33 TLE -
testcase_34 AC 466 ms
5,844 KB
testcase_35 AC 1,909 ms
8,096 KB
testcase_36 TLE -
testcase_37 AC 28 ms
5,720 KB
testcase_38 AC 122 ms
8,276 KB
testcase_39 AC 28 ms
5,708 KB
testcase_40 AC 89 ms
8,372 KB
testcase_41 AC 40 ms
9,036 KB
testcase_42 AC 65 ms
9,308 KB
testcase_43 AC 97 ms
7,220 KB
testcase_44 AC 14 ms
7,536 KB
testcase_45 AC 24 ms
6,824 KB
testcase_46 AC 42 ms
7,300 KB
testcase_47 AC 104 ms
7,104 KB
testcase_48 AC 178 ms
9,232 KB
testcase_49 AC 57 ms
9,332 KB
testcase_50 AC 4 ms
4,376 KB
testcase_51 AC 3 ms
4,396 KB
testcase_52 AC 3 ms
4,376 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 3 ms
4,380 KB
testcase_55 AC 8 ms
6,640 KB
testcase_56 AC 3 ms
4,380 KB
testcase_57 AC 694 ms
7,024 KB
testcase_58 AC 13 ms
8,092 KB
testcase_59 AC 12 ms
7,508 KB
testcase_60 AC 50 ms
6,756 KB
testcase_61 AC 3 ms
4,380 KB
testcase_62 AC 11 ms
8,768 KB
testcase_63 AC 12 ms
8,988 KB
testcase_64 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int DY[]{1, 0, -1, 0}, DX[]{0, -1, 0, 1};
constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}, DX8[]{0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

struct Manacher {
  template <typename T>
  Manacher(const T &s) {
    T str;
    int n = s.size();
    for (int i = 0; i < n; ++i) {
      str.push_back(s[i]);
      if (i + 1 < n) str.push_back('$');
    }
    n = str.size();
    radius.resize(n);
    int j = 1;
    for (int i = 0; i < n;) {
      while (i - j >= 0 && i + j < n && str[i - j] == str[i + j]) ++j;
      radius[i] = j;
      int k = 1;
      while (i - k >= 0 && i + k < n && k + radius[i - k] < j) {
        radius[i + k] = radius[i - k];
        ++k;
      }
      i += k;
      j -= k;
    }
  }

  int odd(int idx) const { return (radius[idx * 2] + 1) / 2; }

  int even(int idx) const { return radius[idx * 2 + 1] / 2; }

  bool is_palindrome(int left, int right) const {
    int mid = (left + right - 1) / 2;
    return (((right - left) & 1) ? odd(mid) * 2 - 1 : even(mid) * 2) >= right - left;
  }

private:
  std::vector<int> radius;
};

// O(M^2)
int solve(int n, const string &s, int m, const string &t) {
  int same = 0;
  for (; same < n && same < m; ++same) {
    if (s[same] != t[same]) break;
  }
  if (same == 0) return INF;
  Manacher manacher(t);
  int ans = 0;
  while (m > same) {
    bool is_updated = false;
    for (int len = m / 2; len > 0; --len) {
      if (manacher.is_palindrome(m - len * 2, m)) {
        m -= len;
        ++ans;
        is_updated = true;
        break;
      }
    }
    if (!is_updated) return INF;
  }
  return ans;
}

int main() {
  int n, m; string s, t; cin >> n >> m >> s >> t;
  if (m % 2 == 1 || !equal(ALL(t), t.rbegin())) {
    cout << "-1\n";
    return 0;
  }
  int ans = solve(n, s, m, t);
  reverse(ALL(s));
  chmin(ans, solve(n, s, m, t));
  cout << (ans == INF ? -1 : ans) << '\n';
  return 0;
}
0