結果

問題 No.672 最長AB列
ユーザー winspyrwinspyr
提出日時 2018-05-06 17:20:06
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 848 bytes
コンパイル時間 425 ms
コンパイル使用メモリ 52,688 KB
実行使用メモリ 6,836 KB
最終ジャッジ日時 2023-09-10 10:46:43
合計ジャッジ時間 1,408 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,496 KB
testcase_01 WA -
testcase_02 AC 2 ms
5,552 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 9 ms
6,648 KB
testcase_10 AC 8 ms
6,644 KB
testcase_11 AC 9 ms
6,744 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 9 ms
6,612 KB
testcase_16 AC 9 ms
6,744 KB
testcase_17 WA -
testcase_18 AC 9 ms
6,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>

using namespace std;

int main()
{
	string s; cin >> s;
	int firstA[200005],secondA[200005];
	int firstB[200005],secondB[200005];
	
	int a = 0,b=0;
	
	for(int i=0;i<s.size()+3;i++)
	{
		firstA[i]=-1;
		secondA[i]=-1;
		firstB[i]=-1;
		secondB[i]=-1;		
	}
	
	firstA[0] = 0;
	firstB[0] = 0;
	
	for(int i=0;i<s.size();i++)
	{
		if(s[i] == 'A') a++;
		else b++;
		
		if(a>=b)
		{
			if(firstA[a-b] == -1)firstA[a-b]=i+1;
			else secondA[a-b] = i+1;	
		}
		else
		{
			if(firstB[b-a] == -1)firstB[b-a]=i+1;
			else secondA[b-a] = i+1;
		}
	}
	
	int ans = 0;
	for(int i=0;i<s.size()+3;i++)
	{
		if(secondA[i] == -1)continue;
		ans = max(ans, secondA[i]-firstA[i]);
	}
	
	for(int i=1;i<s.size();i++)
	{
		if(secondB[i] == -1)continue;
		ans = max(ans, secondB[i]-firstB[i]);
	}
	
	cout << ans << endl;
	
	return 0;
	
}
0