結果

問題 No.2858 Make a Palindrome
ユーザー shobonvipshobonvip
提出日時 2024-08-25 15:43:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 665 ms / 3,000 ms
コード長 2,870 bytes
コンパイル時間 4,091 ms
コンパイル使用メモリ 264,720 KB
実行使用メモリ 16,272 KB
最終ジャッジ日時 2024-08-25 15:43:18
合計ジャッジ時間 11,054 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 665 ms
6,816 KB
testcase_01 AC 174 ms
6,940 KB
testcase_02 AC 181 ms
6,940 KB
testcase_03 AC 168 ms
6,944 KB
testcase_04 AC 173 ms
6,944 KB
testcase_05 AC 145 ms
6,944 KB
testcase_06 AC 121 ms
6,940 KB
testcase_07 AC 122 ms
6,940 KB
testcase_08 AC 118 ms
6,940 KB
testcase_09 AC 121 ms
6,940 KB
testcase_10 AC 159 ms
6,944 KB
testcase_11 AC 154 ms
6,940 KB
testcase_12 AC 152 ms
6,940 KB
testcase_13 AC 150 ms
6,944 KB
testcase_14 AC 173 ms
6,944 KB
testcase_15 AC 79 ms
6,940 KB
testcase_16 AC 82 ms
6,944 KB
testcase_17 AC 83 ms
6,944 KB
testcase_18 AC 83 ms
6,940 KB
testcase_19 AC 81 ms
6,940 KB
testcase_20 AC 87 ms
14,956 KB
testcase_21 AC 85 ms
15,156 KB
testcase_22 AC 48 ms
9,328 KB
testcase_23 AC 65 ms
15,028 KB
testcase_24 AC 70 ms
12,116 KB
testcase_25 AC 63 ms
11,152 KB
testcase_26 AC 46 ms
10,940 KB
testcase_27 AC 98 ms
15,980 KB
testcase_28 AC 68 ms
11,816 KB
testcase_29 AC 70 ms
15,340 KB
testcase_30 AC 109 ms
16,272 KB
testcase_31 AC 97 ms
16,236 KB
testcase_32 AC 103 ms
16,088 KB
testcase_33 AC 98 ms
16,120 KB
testcase_34 AC 98 ms
16,204 KB
testcase_35 AC 100 ms
16,100 KB
testcase_36 AC 114 ms
16,096 KB
testcase_37 AC 98 ms
16,172 KB
testcase_38 AC 98 ms
16,188 KB
testcase_39 AC 97 ms
16,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

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_odd(string s) {
	vector<int> targ = manacher(s);
	int odd = max(targ) * 2 - 1;
	return odd;
}

int max_even(string s) {
	string t = "";
	rep(i,0,(int)s.size()){
		t += s[i];
		t += '$';
	}
	t.pop_back();
	int even = 0;
	vector<int> tmp = manacher(t);
	for (int i=1; i<(int)tmp.size(); i+=2){
		even = max(even, tmp[i]/2*2);
	}
	return even;
}

int max_length_palindrome(string s){
	return max(max_even(s), max_odd(s));
}

void solve(){
	int n; cin >> n;
	ll m; cin >> m;
	
	string s; cin >> s;
	if (max_length_palindrome(s) >= m){
		cout << 1 << endl;
		return;
	}

	string t1 = s;
	string t2 = s;
	t2 += s;
	string t3 = s;
	t3 += s;
	t3 += s;
	string t4 = s;
	t4 += s;
	t4 += s;
	t4 += s;

	if (max_length_palindrome(t2) >= m){
		cout << 2 << endl;
		return;
	}

	if (max_length_palindrome(t3) >= m){
		cout << 3 << endl;
		return;
	}

	if (max_length_palindrome(t3) >= m){
		cout << 3 << endl;
		return;
	}

	if (max_length_palindrome(t2) == max_length_palindrome(t3)){
		cout << -1 << endl;
		return;
	}

	ll a1 = - max_length_palindrome(t2) + max_length_palindrome(t3);
	ll b1 = max_length_palindrome(t2); // 2
	ll f1 = (m - b1 + a1 - 1) / a1 + 2;
	cout << f1 << endl;

	/*	
	ll a2 = - max_length_palindrome(t2) + max_length_palindrome(t4);
	ll b2 = max_length_palindrome(t2); // 3
	ll f2 = (m - b2 + a2 - 1) / a2 * 2 + 3;
	cout << f2 << endl;
	*/

	

}

int main(){

	int t; cin >> t;
	while(t--) solve();
}

0