結果

問題 No.672 最長AB列
ユーザー ohreitetsuohreitetsu
提出日時 2018-04-22 17:59:57
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,338 bytes
コンパイル時間 526 ms
コンパイル使用メモリ 63,452 KB
実行使用メモリ 9,844 KB
最終ジャッジ日時 2023-09-10 04:52:47
合計ジャッジ時間 4,375 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string> 
using namespace std;
void setLens(string S,vector<pair<int,int>> &lenVector)
{
	int i;
	int maxLen=0;
	string maxString;
	int aNum=0,bNum=0;
	int len=1;
	for (i=0;i<S.size();i++){
		if (S[i]=='A'){
			aNum++;
		}else{
			bNum++;
		}
		pair<int,int> num;
		num.first=aNum;
		num.second=bNum;
		lenVector.push_back(num);
	}
}
int GetMaxLen(vector<pair<int,int>> &lenVector,char ch){
	int i;
	int MaxLen=0;
	for (i=0;i<lenVector.size();i++){
		if (ch=='A'){
			lenVector[i].first--;
		}else if (ch=='B'){
			lenVector[i].second--;
		}
		if (lenVector[i].first==lenVector[i].second){
			if (MaxLen<2*lenVector[i].first){
				MaxLen=2*lenVector[i].first;
			}
		}
	}
	if (lenVector.size()>0){
		lenVector.erase(lenVector.begin());
	}
	return MaxLen;
}
int main(int argc, char* argv[])
{
	string S;
	cin>>S;
	int len=S.size();
	int aNum=0,bNum=0;
	int i=0;
	for (i=0;i<len;i++){
		if (S[i]=='A'){
			aNum++;
		}else{
			bNum++;
		}
	}
	if (aNum==0 || bNum==0){
		cout<<0<<endl;
		return 0;
	}else if (aNum==bNum){
		cout<<len<<endl;
		return 0;
	}
	vector<pair<int,int>> lenVector;
	setLens(S,lenVector);
	int MaxLen=GetMaxLen(lenVector,' ');		
	for (i=0;i<len;i++){
		int maxLen=GetMaxLen(lenVector,S[i]);
		if (MaxLen<maxLen){
			MaxLen=maxLen;
		}
	}
	cout<<MaxLen<<endl;
	return 0;
}
0