結果

問題 No.2858 Make a Palindrome
ユーザー ripityripity
提出日時 2024-08-25 16:30:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,945 bytes
コンパイル時間 2,161 ms
コンパイル使用メモリ 210,744 KB
実行使用メモリ 10,332 KB
最終ジャッジ日時 2024-08-25 16:30:10
合計ジャッジ時間 5,564 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 447 ms
6,812 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 18 ms
9,292 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 13 ms
7,744 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 17 ms
9,596 KB
testcase_30 WA -
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 -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;

template <long long MOD>
struct rolling_hash {
	long long base;
	vector<long long> base_pow, hash_prefix;
	rolling_hash(string str, long long _base) {
		base = _base;
		base_pow.resize(str.size()+1, 1);
		hash_prefix.resize(str.size()+1, 0);
		for( int i = 1; i <= str.size(); i++ ) base_pow[i] = (base_pow[i-1]*base)%MOD;
		for( int i = 1; i <= str.size(); i++ ) hash_prefix[i] = (hash_prefix[i-1]*base+str[i-1])%MOD;
	}
	long long hash(int l, int r) {
		return (MOD*MOD+hash_prefix[r]-hash_prefix[l]*base_pow[r-l])%MOD;
	}
};

vector< int > manacher(const string s) {
  vector< int > radius(s.size());
  int i = 0, j = 0;
  while(i < s.size()) {
    while(i - j >= 0 && i + j < s.size() && s[i - j] == s[i + j]) {
      ++j;
    }
    radius[i] = j;
    int k = 1;
    while(i - k >= 0 && i + k < s.size() && k + radius[i - k] < j) {
      radius[i + k] = radius[i - k];
      ++k;
    }
    i += k;
    j -= k;
  }
  return radius;
}

int max_pal(string S) {
  vector<int> ma = manacher(S);
  return *max_element(ma.begin(), ma.end());
}

void solve() {
  int N;
  ll M;
  string S;
  cin >> N >> M >> S;
  if(max_pal(S) >= M) {
    cout << 1 << "\n";
    return;
  }else if(max_pal(S + S) >= M) {
    cout << 2 << "\n";
    return;
  }
  string T = S;
  reverse(T.begin(), T.end());
  mt19937 rng(time(NULL));
  const ll B = 30 % 998244353;
  rolling_hash<1000000007> rh7(S, B);
  rolling_hash<1000000009> rh9(T, B);
  ll INF = 1LL << 60, ans = INF;
  for(int i = 0; i <= N; i++) {
    bool pref = rh7.hash(0, i) == rh9.hash(N - i, N);
    bool suff = rh7.hash(i, N) == rh9.hash(0, N - i);
    // cout << rh7.hash(i, N) << " " << rh9.hash(0, N - i) << endl;
    if(pref && suff) {
      ll k = max(i, N - i);
      ans = min(ans, (M + N - k + N - 1) / N);
    }
  }
  cout << (ans == INF ? -1LL : ans) << endl;
}

int main() {
  int T;
  cin >> T;
  while(T--) solve();
}
0