結果
| 問題 | No.1695 Mirror Mirror |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-10-27 14:26:59 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 12 ms / 2,000 ms |
| コード長 | 3,086 bytes |
| コンパイル時間 | 1,087 ms |
| コンパイル使用メモリ | 111,648 KB |
| 実行使用メモリ | 7,716 KB |
| 最終ジャッジ日時 | 2025-10-27 14:27:04 |
| 合計ジャッジ時間 | 4,322 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 61 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:62:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
62 | scanf("%s", S);
| ~~~~~^~~~~~~~~
main.cpp:63:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
63 | scanf("%s", T);
| ~~~~~^~~~~~~~~
ソースコード
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
// |as| = n ==> |rs| = 2 n + 1
// [i - rs[i], i + rs[i]] is palindrome for $ as[0] $ as[1] $ ... $ as[n-1] $
// as[i, j): palindrome <=> j - i <= rs[i + j]
template <class String> vector<int> manacher(const String &as) {
const int n = as.size();
vector<int> rs(2 * n + 1);
for (int i = 0, j = 0, k; i <= 2 * n; i += k, j -= k) {
for (; 0 < i - j && i + j < 2 * n &&
(!((i + j + 1) & 1) || as[(i - j - 1) >> 1] == as[(i + j + 1) >> 1]);
++j) {}
rs[i] = j;
for (k = 1; k < j && k + rs[i - k] < j; ++k) rs[i + k] = rs[i - k];
}
return rs;
}
int SLen, TLen;
char S[500'010];
char T[500'010];
int main() {
for (; ~scanf("%d%d", &SLen, &TLen); ) {
scanf("%s", S);
scanf("%s", T);
bool isPalT = true;
isPalT = isPalT && (TLen % 2 == 0);
for (int l = 0, r = TLen - 1; l < r; ++l, --r) {
isPalT = isPalT && (T[l] == T[r]);
}
int common = 0;
for (int dir = 0; dir < 2; ++dir) {
for (int i = 0; ; ++i) {
if (!(i < SLen && i < TLen && S[i] == T[i])) {
chmax(common, i);
break;
}
}
reverse(S, S + SLen);
}
int ans = -1;
if (isPalT) {
const auto rs = manacher(string(T, T + TLen/2));
// longest even pal suf
vector<int> fs(TLen/2 + 1, 0);
for (int i = 0; i <= TLen/2; ++i) chmax(fs[i + rs[i*2] / 2], rs[i*2]);
for (int i = TLen/2; --i >= 0; ) chmax(fs[i], fs[i + 1] - 2);
//cerr<<"T/2 = "<<string(T,T+TLen/2)<<endl;
//cerr<<"fs = "<<fs<<endl;
int cost = 1;
for (int n = TLen/2; ; ) {
//cerr<<"cost = "<<cost<<", n = "<<n<<endl;
if (n <= common) {
ans = cost;
break;
}
// fold suf
if (!fs[n]) break;
++cost;
n -= fs[n] / 2;
}
}
printf("%d\n", ans);
}
return 0;
}