結果

問題 No.2454 Former < Latter
ユーザー 沙耶花沙耶花
提出日時 2023-09-02 12:02:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 259 ms / 2,000 ms
コード長 2,419 bytes
コンパイル時間 6,626 ms
コンパイル使用メモリ 261,700 KB
実行使用メモリ 15,096 KB
最終ジャッジ日時 2023-09-02 12:02:47
合計ジャッジ時間 7,830 ms
ジャッジサーバーID
(参考情報)
judge16 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 100 ms
14,980 KB
testcase_03 AC 100 ms
15,036 KB
testcase_04 AC 84 ms
14,980 KB
testcase_05 AC 81 ms
15,068 KB
testcase_06 AC 74 ms
13,956 KB
testcase_07 AC 77 ms
14,196 KB
testcase_08 AC 36 ms
4,380 KB
testcase_09 AC 45 ms
6,332 KB
testcase_10 AC 52 ms
6,776 KB
testcase_11 AC 100 ms
15,032 KB
testcase_12 AC 102 ms
15,032 KB
testcase_13 AC 99 ms
15,096 KB
testcase_14 AC 100 ms
15,008 KB
testcase_15 AC 66 ms
9,548 KB
testcase_16 AC 67 ms
9,412 KB
testcase_17 AC 259 ms
4,384 KB
testcase_18 AC 30 ms
4,380 KB
testcase_19 AC 74 ms
13,988 KB
testcase_20 AC 77 ms
14,040 KB
testcase_21 AC 38 ms
4,384 KB
testcase_22 AC 47 ms
5,532 KB
testcase_23 AC 48 ms
7,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

struct rolling_hash{

	long long t_hash;
	static inline vector<long long> power;
	static const long long MOD = (1LL<<61)-1;
	static const long long b = 123456;
	int sz;
	
	rolling_hash(){		
		sz = 0;
		t_hash = 0;
	}
	
	rolling_hash(char c){
		sz = 1;
		t_hash = b*c;
	}

	long long mul(__int128 x,__int128 y){
		__int128 t = x*y;
		t = (t>>61) + (t&MOD);
		
		if(t>=MOD)t -= MOD;
		return t;
	}
	
	long long get_pow(int sz){
		if(power.size()>sz)return power[sz];
		
		while(power.size()<=sz){
			if(power.size()==0)power.push_back(1);
			else power.push_back(mul(power.back(),b));
		}
		return power.back();
		
	}
	
	rolling_hash &operator+=(const rolling_hash &another){
		
		(*this).t_hash = mul((*this).t_hash,get_pow(another.sz));
		(*this).t_hash += another.t_hash;
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz += another.sz;
		
		return (*this);
	}
	
	rolling_hash operator+(const rolling_hash &another)const{
		return (rolling_hash(*this)+=another);
	}
	
	rolling_hash &operator-=(const rolling_hash &another){

		(*this).t_hash += MOD - mul(another.t_hash,get_pow((*this).sz-another.sz));
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz -= another.sz;

		return (*this);
	}
	
	rolling_hash operator-(const rolling_hash &another)const{
		return (rolling_hash(*this)-=another);
	}
	
	bool operator<(const rolling_hash &another)const{
		if((*this).t_hash!=another.t_hash)return (*this).t_hash<another.t_hash;
		return (*this).sz<another.sz;
	}
	
	bool operator==(const rolling_hash &another)const{
		return ((*this).t_hash==another.t_hash && (*this).sz==another.sz);
	}

	
};

int main(){
	
	int _t;
	cin>>_t;
	
	rep(_,_t){
		
		int n;
		cin>>n;
		string s;
		cin>>s;
		
		vector<rolling_hash> r(n+1);
		rep(i,n){
			r[i+1] = r[i] + rolling_hash(s[i]);
		}
		auto R = r;
		int ans = 0;
		
		for(int i=1;i<n;i++){
			int ok = 0,ng = min(i,n-i) + 1;
			while(ng-ok>1){
				int mid = (ok+ng)/2;
				if(R[mid] == R[i+mid]-R[i]){
					ok = mid;
				}
				else ng = mid;
				
			}
			if(ok == i){
				if(i<n-i)ans++;
			}
			else{
				if(i+ok>=n)continue;
				if(s[ok]<s[i+ok])ans++;
			}
		}
		cout<<ans<<endl;
	}
		
	return 0;
}
0