#include <iostream>
#include <map>
#include <tuple>
#include <string>
using namespace std;
typedef long long ll;
pair<int, int> abcpos;
// logical value is phys - abcpos
map<pair<int, int>, int> abc;
struct shiftmap {
int shift = 0;
map<int, int> xy;
void x() {
xy[shift]++;
shift--;
}
void y() {
xy[shift]++;
shift++;
}
void z() {
xy.clear();
shift = 0;
}
int get() {
return xy[shift];
}
};
shiftmap ab, ac, bc;
string s;
ll total;
int main() {
ios::sync_with_stdio(false); cin.tie();
cin >> s;
int run = 0;
char runchar = 0;
for (char c : s) {
if (runchar == c) run++;
else run = 1, runchar = c;
total += run;
abc[abcpos]++;
switch (c) {
case 'a': abcpos.first++; abcpos.second++; ab.x(); ac.x(); bc.z(); break;
case 'b': abcpos.first--; ab.y(); ac.z(); bc.x(); break;
case 'c': abcpos.second--; ab.z(); ac.y(); bc.y(); break;
}
total += abc[abcpos];
total += ab.get();
total += ac.get();
total += bc.get();
}
cout << total << '\n';
}
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 | #include <iostream> #include <map> #include <tuple> #include <string> using namespace std; typedef long long ll; pair<int, int> abcpos; // logical value is phys - abcpos map<pair<int, int>, int> abc; struct shiftmap { int shift = 0; map<int, int> xy; void x() { xy[shift]++; shift--; } void y() { xy[shift]++; shift++; } void z() { xy.clear(); shift = 0; } int get() { return xy[shift]; } }; shiftmap ab, ac, bc; string s; ll total; int main() { ios::sync_with_stdio(false); cin.tie(); cin >> s; int run = 0; char runchar = 0; for (char c : s) { if (runchar == c) run++; else run = 1, runchar = c; total += run; abc[abcpos]++; switch (c) { case 'a': abcpos.first++; abcpos.second++; ab.x(); ac.x(); bc.z(); break; case 'b': abcpos.first--; ab.y(); ac.z(); bc.x(); break; case 'c': abcpos.second--; ab.z(); ac.y(); bc.y(); break; } total += abc[abcpos]; total += ab.get(); total += ac.get(); total += bc.get(); } cout << total << '\n'; } |
English