結果

問題 No.2858 Make a Palindrome
ユーザー Yudai0606Nazo
提出日時 2025-08-02 06:22:51
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 614 ms / 3,000 ms
コード長 2,945 bytes
コンパイル時間 4,582 ms
コンパイル使用メモリ 257,652 KB
実行使用メモリ 16,652 KB
最終ジャッジ日時 2025-08-02 06:23:01
合計ジャッジ時間 9,759 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <cassert>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using mint = modint998244353;
//using mint = modint1000000007;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repu(i, s, t) for (int i = (int)(s); i < (int)(t); i++)
#define repd(i, s, t) for (int i = (int)(s)-1; i >= (int)(t); i--)
#define all(v) v.begin(), v.end()
void _u() { cerr << endl; } template <class H, class... T> void _u(H&& h, T&&... t) { cerr << h << ", "; _u(move(t)...); }
#define U(...) { cerr << #__VA_ARGS__ << ": "; _u(__VA_ARGS__); }
template<typename T> bool chmax(T &a, const T b) { if(a >= b) return false; a = b; return true; }
template<typename T> bool chmin(T &a, const T b) { if(a <= b) return false; a = b; return true; }
template<typename T> istream& operator>>(istream &in, vector<T> &a) { for(T &x: a) in >> x; return in; }
template<typename T> ostream& operator<<(ostream &out, const vector<T> &a) { for(const T &x: a) out << x << ' '; return out; }
const int di[] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
const int dj[] = {0, 1, 0, -1, -1, 1, 1, -1, 0};


struct RH {
    string s;
    vector<mint> hash;
    vector<mint> msk;

    RH(): s("") {}
    RH(string s_): s(s_) { make_hash(); }
    void make_hash() {
        hash.push_back(0);
        mint tmp1 = 0;
        mint tmp2 = 1;
        rep(i, s.size()) {
            tmp1 += s[i];
            hash.push_back(tmp1);
            msk.push_back(tmp2);
            tmp1 *= mint(1 << 8);
            tmp2 = (ll(tmp2.val()) << 8);
        }
        msk.push_back(tmp2);
    }
    mint substr(int l, int r) {
        return hash[r] - hash[l] * msk[r - l];
    }
};

const ll INF = 1e18;
ll solve() {
    int n, m;
    string s;
    cin >> n >> m >> s;
    RH h1(s), h2(s+s);
    reverse(all(s));
    RH r1(s), r2(s+s);

    ll ans = INF;
    rep(i, n-m+1) if(h1.substr(i, i+m) == r1.substr(n-i-m, n-i)) chmin(ans, 1LL);
    rep(i, n-m) if(h1.substr(i, i+m+1) == r1.substr(n-i-m-1, n-i)) chmin(ans, 1LL);
    rep(i, 2*n-m+1) if(h2.substr(i, i+m) == r2.substr(2*n-i-m, 2*n-i)) chmin(ans, 2LL);
    rep(i, 2*n-m) if(h2.substr(i, i+m+1) == r2.substr(2*n-i-m-1, 2*n-i)) chmin(ans, 2LL);

    auto calc = [&](int x, int y) {
        int tmp = m - x;
        if(tmp <= 0) return 1LL;
        ll res = INF;
        chmin(res, ll(tmp + 2*n - 1) / (2*n) * 2 + 1);
        tmp -= 2*y;
        if(tmp <= 0) return 2LL;
        chmin(res, ll(tmp + 2*n - 1) / (2*n) * 2 + 2);
        return res;
    };
    rep(i, n) {
        if(h1.substr(0, i) != r1.substr(n-i, n)) continue;
        if(h1.substr(i, n) != r1.substr(0, n-i)) continue;
        chmin(ans, calc(i, n-i));
        chmin(ans, calc(n-i, i));
    }
    return ans;
}

int main() {
    int t;
    cin >> t;
    rep(i, t) {
        ll res = solve();
        cout << (res < INF ? res : -1LL) << endl;
    }
    return 0;
}
0