#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; void init_io() { cin.tie(nullptr); ios::sync_with_stdio(false); } constexpr int dims = 3; int64_t solve_dir(const string &word, const unsigned dir) { int step = 0; array<int, dims> adjustment = {}; REP(i, dims) if (dir & (1u << i)) { step += 1; adjustment[i] -= 1; } array<int, dims> state = {}; vector<array<int, dims-1>> points; points.reserve(word.size() + 1); auto it = word.begin(); for(;;) { array<int, dims-1> point; REP(i, dims-1) point[i] = state[i]; points.push_back(point); if (it == word.end()) break; const char c = *it++; state[c - 'a'] += step; REP(i, dims) state[i] += adjustment[i]; } sort(points.begin(), points.end()); int64_t res = 0; auto p = points.begin(); while (p != points.end()) { auto q = p + 1; while (q != points.end() && *q == *p) ++q; int64_t n = q - p; res += n * (n-1) / 2; p = q; } return res; } int64_t solve(const string &word) { int64_t res = 0; for (unsigned dir = 1; dir < (1u << dims); ++dir) { res += solve_dir(word, dir); } return res; } int main() { init_io(); string word; word.reserve(300000); cin >> word; cout << solve(word) << "\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 66 67 68 69 70 71 72 | #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; void init_io() { cin.tie(nullptr); ios::sync_with_stdio(false); } constexpr int dims = 3; int64_t solve_dir(const string &word, const unsigned dir) { int step = 0; array<int, dims> adjustment = {}; REP(i, dims) if (dir & (1u << i)) { step += 1; adjustment[i] -= 1; } array<int, dims> state = {}; vector<array<int, dims-1>> points; points.reserve(word.size() + 1); auto it = word.begin(); for(;;) { array<int, dims-1> point; REP(i, dims-1) point[i] = state[i]; points.push_back(point); if (it == word.end()) break; const char c = *it++; state[c - 'a'] += step; REP(i, dims) state[i] += adjustment[i]; } sort(points.begin(), points.end()); int64_t res = 0; auto p = points.begin(); while (p != points.end()) { auto q = p + 1; while (q != points.end() && *q == *p) ++q; int64_t n = q - p; res += n * (n-1) / 2; p = q; } return res; } int64_t solve(const string &word) { int64_t res = 0; for (unsigned dir = 1; dir < (1u << dims); ++dir) { res += solve_dir(word, dir); } return res; } int main() { init_io(); string word; word.reserve(300000); cin >> word; cout << solve(word) << "\n"; } |