fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long
  3. #define endl "\n"
  4. #define F first
  5. #define S second
  6. #define loop(a,n) for(int i=a; i<=n ; i++)
  7. #define TIME (1.0 * clock() / CLOCKS_PER_SEC)
  8. #define NAME "Dance"
  9. using namespace std;
  10. int a[1000],f[1000],n,res=0;
  11. string s;
  12. void nhap(){
  13. cin >> n;
  14. cin>>s;
  15. s=' '+s;
  16. for(int i=1;i<=n;++i) {
  17. if(s[i]=='A' || s[i] == 'a') a[i] = -1;
  18. if(s[i]=='B' || s[i] == 'b') a[i] = 1;
  19. }
  20. }
  21. void solve(){
  22. a[0]=0;
  23. f[0]=0;
  24. for(int i=1;i<=n;++i) f[i]=f[i-1]+a[i];
  25. for(int i=1;i<=n-1;++i) {
  26. for(int j=i+1;j<=n;++j) {
  27. if (f[j] - f[i-1]==0) res++;
  28. }
  29. }
  30. cout<<res;
  31. }
  32.  
  33. int main(){
  34. ios_base::sync_with_stdio(0);
  35. cin.tie(0);cout.tie(0);
  36. freopen(NAME".INP","r",stdin);
  37. freopen(NAME".OUT","w",stdout);
  38. nhap();
  39. solve();
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty