#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
struct Trzy{
int A,B,C;
};
struct comp {
bool operator()(const Trzy& x,const Trzy& y) const {
if(x.A > y.A) return true;
else if(x.A < y.A) return false;
else if(x.B < y.B) return false;
else if(x.B > y.B) return true;
else if(x.C > y.C) return true;
else return false;
}
};
long long funkcja(vector<Trzy> X) {
map<Trzy,long long,comp> M;
long long ans = 0;
for(int i=0; i<X.size(); ++i) {
M[X[i]] = 0;
}
for(int i=0; i<X.size(); ++i) {
M[X[i]]++;
}
for(auto it = M.begin(); it != M.end(); ++it) {
ans += (it->second)*(it->second - 1)/2;
}
return ans;
}
int main() {
string S;
cin>>S;
vector<Trzy> V;
Trzy t;
t.A = t.B = t.C = 0;
V.push_back(t);
for(int i=0; i<S.size(); ++i) {
if(S[i] == 'a') t.A++;
else if(S[i] == 'b') t.B++;
else t.C++;
V.push_back(t);
}
vector<Trzy> abc,ab,bc,ac;
for(int i=0; i<V.size(); ++i) {
int Min;
Min = min(min(V[i].A,V[i].B),V[i].C);
t.A = V[i].A - Min;
t.B = V[i].B - Min;
t.C = V[i].C - Min;
abc.push_back(t);
t.A = V[i].A - V[i].B;
t.B = 0;
t.C = V[i].C;
ab.push_back(t);
t.A = V[i].A;
t.B = V[i].B - V[i].C;
t.C = 0;
bc.push_back(t);
t.A = 0;
t.B = V[i].B;
t.C = V[i].A - V[i].C;
ac.push_back(t);
}
long long odp = 0;
odp = funkcja(abc) + funkcja(ab) + funkcja(bc) + funkcja(ac);
char Z = '?';
for(int i=0; i<S.size(); ) {
Z = S[i];
long long Q = 0;
do {
i++;
Q++;
} while(Z == S[i]);
odp += Q + Q*(Q-1) / 2;
}
cout<<odp<<endl;
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <iostream> #include <vector> #include <map> #include <string> #include <cmath> #include <algorithm> using namespace std; struct Trzy{ int A,B,C; }; struct comp { bool operator()(const Trzy& x,const Trzy& y) const { if(x.A > y.A) return true; else if(x.A < y.A) return false; else if(x.B < y.B) return false; else if(x.B > y.B) return true; else if(x.C > y.C) return true; else return false; } }; long long funkcja(vector<Trzy> X) { map<Trzy,long long,comp> M; long long ans = 0; for(int i=0; i<X.size(); ++i) { M[X[i]] = 0; } for(int i=0; i<X.size(); ++i) { M[X[i]]++; } for(auto it = M.begin(); it != M.end(); ++it) { ans += (it->second)*(it->second - 1)/2; } return ans; } int main() { string S; cin>>S; vector<Trzy> V; Trzy t; t.A = t.B = t.C = 0; V.push_back(t); for(int i=0; i<S.size(); ++i) { if(S[i] == 'a') t.A++; else if(S[i] == 'b') t.B++; else t.C++; V.push_back(t); } vector<Trzy> abc,ab,bc,ac; for(int i=0; i<V.size(); ++i) { int Min; Min = min(min(V[i].A,V[i].B),V[i].C); t.A = V[i].A - Min; t.B = V[i].B - Min; t.C = V[i].C - Min; abc.push_back(t); t.A = V[i].A - V[i].B; t.B = 0; t.C = V[i].C; ab.push_back(t); t.A = V[i].A; t.B = V[i].B - V[i].C; t.C = 0; bc.push_back(t); t.A = 0; t.B = V[i].B; t.C = V[i].A - V[i].C; ac.push_back(t); } long long odp = 0; odp = funkcja(abc) + funkcja(ab) + funkcja(bc) + funkcja(ac); char Z = '?'; for(int i=0; i<S.size(); ) { Z = S[i]; long long Q = 0; do { i++; Q++; } while(Z == S[i]); odp += Q + Q*(Q-1) / 2; } cout<<odp<<endl; return 0; } |
English