#include <iostream>
#include <cstring>
using namespace std;
bool isBalanced(string s)
{
int a=0,b=0,c=0;
for(int i=0;i<=s.size();i++)
{
if(s.substr(i,1)=="a")
a++;
else if(s.substr(i,1)=="b")
b++;
else if(s.substr(i,1)=="c")
c++;
}
if(a*b*c==0)
{
if(a==b)
return true;
else if(a==c)
return true;
else if(b==c)
return true;
else
return false;
}
else if(a==b && b==c && a==c)
return true;
else
return false;
}
int counting(string s)
{
int a=0;
for(int i=0;i<s.size();i++)
for(int j=1;j<=s.size()-i;j++)
{
if(isBalanced(s.substr(i,j)))
a++;
}
return a;
}
int main()
{
string word;
cin >> word;
cout << counting(word);
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <iostream> #include <cstring> using namespace std; bool isBalanced(string s) { int a=0,b=0,c=0; for(int i=0;i<=s.size();i++) { if(s.substr(i,1)=="a") a++; else if(s.substr(i,1)=="b") b++; else if(s.substr(i,1)=="c") c++; } if(a*b*c==0) { if(a==b) return true; else if(a==c) return true; else if(b==c) return true; else return false; } else if(a==b && b==c && a==c) return true; else return false; } int counting(string s) { int a=0; for(int i=0;i<s.size();i++) for(int j=1;j<=s.size()-i;j++) { if(isBalanced(s.substr(i,j))) a++; } return a; } int main() { string word; cin >> word; cout << counting(word); return 0; } |
English