結果

問題 No.1695 Mirror Mirror
ユーザー 👑 emthrmemthrm
提出日時 2021-10-01 23:01:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,726 bytes
コンパイル時間 2,283 ms
コンパイル使用メモリ 206,156 KB
実行使用メモリ 15,168 KB
最終ジャッジ日時 2023-09-26 21:52:51
合計ジャッジ時間 5,282 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 WA -
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 11 ms
8,616 KB
testcase_22 AC 11 ms
9,852 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 12 ms
12,296 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 13 ms
13,652 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 3 ms
4,376 KB
testcase_51 AC 3 ms
4,380 KB
testcase_52 AC 3 ms
4,380 KB
testcase_53 AC 2 ms
4,380 KB
testcase_54 AC 2 ms
4,376 KB
testcase_55 AC 8 ms
7,404 KB
testcase_56 AC 8 ms
8,376 KB
testcase_57 AC 7 ms
6,384 KB
testcase_58 AC 14 ms
13,256 KB
testcase_59 AC 14 ms
14,504 KB
testcase_60 AC 7 ms
6,876 KB
testcase_61 AC 5 ms
5,456 KB
testcase_62 WA -
testcase_63 WA -
testcase_64 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

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;

template <typename T = char, typename U = std::string>
std::vector<std::pair<T, int>> run_length_encoding(const U &s) {
  int n = s.size();
  std::vector<std::pair<T, int>> res;
  T now = s[0];
  int cnt = 1;
  for (int i = 1; i < n; ++i) {
    if (s[i] != now) {
      res.emplace_back(now, cnt);
      cnt = 0;
    }
    now = s[i];
    ++cnt;
  }
  res.emplace_back(now, cnt);
  return res;
}

int solve(int n, const string &s, const vector<pair<char, int>> &t) {
  if (s.front() != t.front().first) return INF;
  const int m = t.size();
  auto rle = run_length_encoding(s);
  int pt = 0, ps = 0, ans = 0;
  while (true) {
    while (ps < rle.size() && pt < m && t[pt] == rle[ps]) ++ps, ++pt;
    if (rle.size() > ps + 1) rle.resize(ps + 1);
    if (pt == m) return ps == rle.size() ? ans : ans + 1;
    ++ans;
    if (ps == rle.size()) {
      --ps, --pt;
      if (ps == 0) return INF;
    }
    if (t[pt].second % 2 == 1) return INF;
    while (rle[ps].second < t[pt].second) {
      rle[ps].second = min(rle[ps].second * 2, t[pt].second);
      if (rle[ps].second < t[pt].second) ++ans;
    }
    rle[ps].second = t[pt].second;
    ++ps;
    ++pt;
    for (int i = ps - 2; i >= 0; --i) {
      rle.emplace_back(rle[i]);
      if (pt == m || rle.back() != t[pt]) break;
      ++ps;
      ++pt;
    }
    // cout << ps << ' ' << pt << " : " << ans << '\n';
    // REP(i, rle.size()) cout << '{' << rle[i].first << ',' << rle[i].second << '}' << " \n"[i + 1 == rle.size()];
  }
  assert(false);
  return INF;
}

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;
  }
  auto rle = run_length_encoding(t);
  int ans = solve(n, s, rle);
  reverse(ALL(s));
  chmin(ans, solve(n, s, rle));
  cout << (ans == INF ? -1 : ans) << '\n';
  return 0;
}
0