結果

問題 No.2454 Former < Latter
ユーザー AerenAeren
提出日時 2023-09-01 22:40:19
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 41 ms / 2,000 ms
コード長 1,638 bytes
コンパイル時間 4,537 ms
コンパイル使用メモリ 358,732 KB
実行使用メモリ 6,012 KB
最終ジャッジ日時 2023-09-01 22:40:28
合計ジャッジ時間 5,273 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 7 ms
5,920 KB
testcase_03 AC 7 ms
5,936 KB
testcase_04 AC 7 ms
5,964 KB
testcase_05 AC 7 ms
5,948 KB
testcase_06 AC 8 ms
5,968 KB
testcase_07 AC 7 ms
5,960 KB
testcase_08 AC 5 ms
4,380 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 7 ms
5,952 KB
testcase_12 AC 7 ms
6,012 KB
testcase_13 AC 7 ms
5,964 KB
testcase_14 AC 7 ms
5,924 KB
testcase_15 AC 6 ms
4,976 KB
testcase_16 AC 6 ms
4,484 KB
testcase_17 AC 41 ms
4,376 KB
testcase_18 AC 6 ms
4,376 KB
testcase_19 AC 7 ms
5,972 KB
testcase_20 AC 8 ms
5,924 KB
testcase_21 AC 7 ms
4,376 KB
testcase_22 AC 6 ms
4,376 KB
testcase_23 AC 6 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

// z[i]: largest prefix(suffix if Reverse) starting(ending if Reverse) at i that is also a proper prefix(suffix if Reverse)
// O(n)
template<class Char_Type, bool Reverse = false>
vector<int> z_function(vector<Char_Type> s){
	if(Reverse) reverse(s.begin(), s.end());
	int n = (int)s.size();
	vector<int> z(n);
	for(auto i = 1, j = 0; i < n; ++ i){
		if(i < j + z[j]) z[i] = min(j + z[j] - i, z[i - j]);
		while(i + z[i] < n && s[z[i]] == s[i + z[i]]) ++ z[i];
		if(i + z[i] > j + z[j]) j = i;
	}
	if(Reverse) reverse(z.begin(), z.end());
	return z;
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::badbit | ios::failbit);
	auto __solve_tc = [&](auto __tc_num)->int{
		int n;
		string s;
		cin >> n >> s;
		auto z = z_function(vector<int>(s.begin(), s.end()));
		int res = 0;
		for(auto i = 1; i < n; ++ i){
			int len = min(z[i], i);
			res += n - i != len && (i == len || s[len] < s[i + len]);
		}
		cout << res << "\n";
		return 0;
	};
	int __tc_cnt;
	cin >> __tc_cnt;
	for(auto __tc_num = 0; __tc_num < __tc_cnt; ++ __tc_num){
		__solve_tc(__tc_num);
	}
	return 0;
}

/*

*/

////////////////////////////////////////////////////////////////////////////////////////
//                                                                                    //
//                                   Coded by Aeren                                   //
//                                                                                    //
////////////////////////////////////////////////////////////////////////////////////////
0