結果

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

テストケース

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

ソースコード

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();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;
			else secondA[a-b] = i;	
		}
		else
		{
			if(firstB[b-a] == -1)firstB[b-a]=i;
			else secondA[b-a] = i;
		}
	}
	
	int ans = 0;
	for(int i=0;i<s.size();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