#include <iostream> #include <unordered_map> #include <map> #include <utility> #include <functional> const long long MM = 300005; const long long MM2 = MM*2; struct V{ int a,b,c; bool operator==(const V& v) const { return a==v.a && b==v.b && c==v.c; } V operator+(const V& r) const { return {a+r.a, b+r.b, c+r.c}; } V operator-(const V& r) const { return {a-r.a, b-r.b, c-r.c}; } V& operator+=(const V& r) { a += r.a; b += r.b; c += r.c; return *this; } V& operator-=(const V& r) { a -= r.a; b -= r.b; c -= r.c; return *this; } // std::tuple<int,int,int> toTuple() const { // return {a,b,c}; // } long long toHash() const { return ((a+MM)*MM2+b+MM)*MM2+c+MM; } }; struct HashV { std::size_t operator()(const V& k) const { return std::hash<long long>()(k.toHash()); } }; V toVa(char c) { if (c=='a') return {0,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {0,0,1}; return {0,0,0}; } V toVb(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,0,0}; else if (c=='c') return {0,0,1}; return {0,0,0}; } V toVc(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {0,0,0}; return {0,0,0}; } V toVab(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {-1,0,0}; else if (c=='c') return {0,0,1}; return {0,0,0}; } V toVac(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {-1,0,0}; return {0,0,0}; } V toVbc(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {0,-1,0}; return {0,0,0}; } V toVabc(char c) { if (c=='a') return {1,0,1}; else if (c=='b') return {-1,1,0}; else if (c=='c') return {0,-1,-1}; return {0,0,0}; } using Func = std::function<V(char)>; int main() { std::ios_base::sync_with_stdio(0); std::string s; std::cin >> s; V v{0,0,0}; long long sol = 0; Func F[] = {toVa,toVb,toVc,toVab,toVac,toVbc,toVabc}; for (auto f : F) { std::unordered_map<V, int, HashV> M; for (char c : s) { V cv = f(c); v -= cv; ++M[cv+v]; sol += M[v]; } } std::cout << sol << "\n"; 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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include <iostream> #include <unordered_map> #include <map> #include <utility> #include <functional> const long long MM = 300005; const long long MM2 = MM*2; struct V{ int a,b,c; bool operator==(const V& v) const { return a==v.a && b==v.b && c==v.c; } V operator+(const V& r) const { return {a+r.a, b+r.b, c+r.c}; } V operator-(const V& r) const { return {a-r.a, b-r.b, c-r.c}; } V& operator+=(const V& r) { a += r.a; b += r.b; c += r.c; return *this; } V& operator-=(const V& r) { a -= r.a; b -= r.b; c -= r.c; return *this; } // std::tuple<int,int,int> toTuple() const { // return {a,b,c}; // } long long toHash() const { return ((a+MM)*MM2+b+MM)*MM2+c+MM; } }; struct HashV { std::size_t operator()(const V& k) const { return std::hash<long long>()(k.toHash()); } }; V toVa(char c) { if (c=='a') return {0,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {0,0,1}; return {0,0,0}; } V toVb(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,0,0}; else if (c=='c') return {0,0,1}; return {0,0,0}; } V toVc(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {0,0,0}; return {0,0,0}; } V toVab(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {-1,0,0}; else if (c=='c') return {0,0,1}; return {0,0,0}; } V toVac(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {-1,0,0}; return {0,0,0}; } V toVbc(char c) { if (c=='a') return {1,0,0}; else if (c=='b') return {0,1,0}; else if (c=='c') return {0,-1,0}; return {0,0,0}; } V toVabc(char c) { if (c=='a') return {1,0,1}; else if (c=='b') return {-1,1,0}; else if (c=='c') return {0,-1,-1}; return {0,0,0}; } using Func = std::function<V(char)>; int main() { std::ios_base::sync_with_stdio(0); std::string s; std::cin >> s; V v{0,0,0}; long long sol = 0; Func F[] = {toVa,toVb,toVc,toVab,toVac,toVbc,toVabc}; for (auto f : F) { std::unordered_map<V, int, HashV> M; for (char c : s) { V cv = f(c); v -= cv; ++M[cv+v]; sol += M[v]; } } std::cout << sol << "\n"; return 0; } |