結果

問題 No.2276 I Want AC
ユーザー HIcoder
提出日時 2023-07-13 12:07:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,221 bytes
コンパイル時間 921 ms
コンパイル使用メモリ 104,688 KB
最終ジャッジ日時 2025-02-15 10:09:26
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
AAAA...AACCC...CCCが最適

*/
#include<iostream>
#include<set>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<numeric>
#include<queue>
#include<cmath>
#include <unordered_set>
using namespace std;
typedef long long ll;
const ll INF=1LL<<60;
typedef pair<int,int> P;
typedef pair<int,P> PP;
const ll MOD=998244353;

int main(){
    int N;
    cin>>N;
    string S;
    cin>>S;

    ll tot=0;
    int cntA=0;
    int cntC=0;
    

    //すべての?をCとしたとき
    for(int i=0;i<N;i++){
        if(S[i]=='?' || S[i]=='C'){
           cntC++;
            //Cにする
            tot+=cntA;
        }else if(S[i]=='A'){
            cntA++;//[0,i]で出現したAの数
        }

    }

    cntA=0;

    ll ans=tot;
    /*
    cntA=[0,i]にあるAの数
    cntC=[i,N-1]にあるCの数
    */

    for(int i=0;i<N;i++){
        if(S[i]=='C'){
            cntC--;
        }
        else if(S[i]=='A'){
            cntA++;
        }else if(S[i]=='?'){
            //この時点で

            tot-=1LL*cntA*cntC;

            cntA++;
            cntC--;

            tot+=1LL*cntA*cntC;

            ans=max(tot,ans);

        }


    }
    
    cout<<ans<<endl;

}
0