結果

問題 No.2454 Former < Latter
ユーザー shobonvipshobonvip
提出日時 2023-09-01 22:03:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 2,819 bytes
コンパイル時間 4,504 ms
コンパイル使用メモリ 263,856 KB
実行使用メモリ 19,344 KB
最終ジャッジ日時 2023-09-01 22:03:53
合計ジャッジ時間 6,822 ms
ジャッジサーバーID
(参考情報)
judge16 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
9,108 KB
testcase_01 AC 15 ms
9,104 KB
testcase_02 AC 43 ms
15,668 KB
testcase_03 AC 38 ms
15,604 KB
testcase_04 AC 48 ms
18,268 KB
testcase_05 AC 46 ms
17,748 KB
testcase_06 AC 48 ms
18,264 KB
testcase_07 AC 43 ms
16,964 KB
testcase_08 AC 50 ms
9,572 KB
testcase_09 AC 39 ms
11,960 KB
testcase_10 AC 44 ms
12,132 KB
testcase_11 AC 38 ms
15,624 KB
testcase_12 AC 43 ms
15,632 KB
testcase_13 AC 38 ms
15,640 KB
testcase_14 AC 38 ms
15,628 KB
testcase_15 AC 59 ms
15,128 KB
testcase_16 AC 57 ms
14,708 KB
testcase_17 AC 84 ms
9,176 KB
testcase_18 AC 58 ms
9,120 KB
testcase_19 AC 63 ms
19,344 KB
testcase_20 AC 62 ms
18,028 KB
testcase_21 AC 54 ms
9,696 KB
testcase_22 AC 44 ms
11,224 KB
testcase_23 AC 43 ms
12,212 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;
}

// isprime
bool isprime(ll n){
	if (n == 1) return false;
	for (ll i = 2; i * i <= n; i++){
		if (n % i == 0) return false;
	}
	return true;
}


// importrandom
// don't forget randomjumon
// and also isprime
ll RandomMod(ll l, ll r){
	ll ret = l + rand() % (r - l);
	while (!isprime(ret)) ret = l + rand() % (r - l);
	return ret;
}

ll randrange(ll l, ll r){
	return l + rand() % (r - l);
}
// -----


int main(){
	srand((unsigned)time(NULL));
	
	// Rolling Hash
	// don't forget randomjumon and importrandom
	typedef dynamic_modint<0> mint1;
	typedef dynamic_modint<1> mint2;
	
	int mod1 = RandomMod(7e8, 1e9);
	int mod2;
	do{
		mod2 = RandomMod(7e8, 1e9);
	}while(mod1 == mod2);
	
	mint1::set_mod(mod1);
	mint2::set_mod(mod2);
	
	mint1 b1 = randrange(100, 200);
	mint2 b2 = randrange(100, 200);
	
	int MAX_m = 800000;
	vector<mint1> b1l(MAX_m+1);
	b1l[0] = 1;
	for (int i=0; i<MAX_m; i++){
		b1l[i+1] = b1l[i] * b1;
	}
	
	vector<mint2> b2l(MAX_m+1);
	b2l[0] = 1;
	for (int i=0; i<MAX_m; i++){
		b2l[i+1] = b2l[i] * b2;
	}
	// -----
	

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int CASES; cin >> CASES;
	while(CASES--){
		int n; cin >> n;
		string s; cin >> s;
		int ans = 0;
		vector<mint1> t1(n+1);
		vector<mint2> t2(n+1);
		rep(i,0,n){
			t1[i+1] = t1[i] * b1 + s[i] - 'a' + 1;
			t2[i+1] = t2[i] * b2 + s[i] - 'a' + 1;
		}
		vector<int> sa = suffix_array(s);
		vector<int> q(n);
		rep(i,0,n){
			q[sa[i]] = i;
		}
		rep(i,1,n){
			int v = min(i, n-i);
			mint1 x1 = t1[v] - t1[0] * b1l[v];
			mint1 y1 = t1[i+v] - t1[i] * b1l[v];
			mint2 x2 = t2[v] - t2[0] * b2l[v];
			mint2 y2 = t2[i+v] - t2[i] * b2l[v];
			if (x2 == y2 && y1 == x1){
				if (i < n-i){
					ans++;
				}
			}else{
				if (q[0] < q[i]){
					ans++;
				}
			}
		}
		cout << ans << '\n';
	}
	
}

0