結果
| 問題 |
No.672 最長AB列
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-04-22 17:59:57 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,338 bytes |
| コンパイル時間 | 549 ms |
| コンパイル使用メモリ | 62,296 KB |
| 実行使用メモリ | 13,880 KB |
| 最終ジャッジ日時 | 2024-06-27 20:32:28 |
| 合計ジャッジ時間 | 4,166 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 9 TLE * 1 -- * 6 |
ソースコード
#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;
}