题解[AT4992] ABC

· · 题解

题意简述:

略。见题目。

思路:

显然单独的C与B是没有用处的。所以我们只保留BC与A。

于是我们可以记录A的个数,如果遇到BC就把结果加上记录的值。如果遇到单独的B或C就把A的个数清零。

code:

#include<iostream>
#include<string>
#define int long long
using namespace std;
signed main(){
   string c;
   cin>>c;
   int tot=0,ans=0;
   for(int i=0;i<c.size();i++){
      if(c[i]=='A')tot++;
      else if(c[i]=='B'){
         if(i+1<c.size()&&c[i+1]=='C'){
            ans+=tot;i++;
         }
         else 
            tot=0;
      }
      else
         tot=0;
   }
   cout<<ans<<endl;
   return 0;
}

tips:

  1. 不开long long见祖宗
  2. 注意下标