結果

問題 No.672 最長AB列
コンテスト
ユーザー vjudge1
提出日時 2026-01-16 14:34:15
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 9 ms / 2,000 ms
コード長 504 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,751 ms
コンパイル使用メモリ 184,736 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-01-16 14:34:18
合計ジャッジ時間 2,824 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
using namespace std;
string s;
const int u=4e5+7;
int n,ans;
int po[u],a[u],b[u];
int main(){
	cin>>s;
	n=s.size();
	s=" "+s;
	for(int i=1;i<=n;i++){
		a[i]=a[i-1]+(s[i]=='A');
		b[i]=b[i-1]+(s[i]=='B');
	} 
	int eps=200000;
	for(int i=0;i<=400000;i++){
		po[i]=n+1;
	po[eps]=0;
	}
	for(int i=1;i<=n;++i){
		int xx=a[i]-b[i]+eps;
		po[xx]=min(po[xx],i);
	}
	for(int r=1;r<=n;r++){
		int xx=a[r]-b[r]+eps;
		if(po[xx]<r){
			ans=max(ans,r-po[xx]);
		}	
	}
	cout<<ans;
	return 0;
}
0