#include <bits/stdc++.h> using namespace std; int ps[3][300300]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; for(int i = 0; i < s.length(); i++) { char l = s[i]; ps['a' - 'a'][i+1] = ps['a' - 'a'][i] + (l=='a'); ps['b' - 'a'][i+1] = ps['b' - 'a'][i] + (l=='b'); ps['c' - 'a'][i+1] = ps['c' - 'a'][i] + (l=='c'); } int r = 0; for(int i = 1; i <= s.length(); i++) { for(int j = i; j <= s.length(); j++) { int ca = ps['a' - 'a'][j] - ps['a' - 'a'][i - 1]; int cb = ps['b' - 'a'][j] - ps['b' - 'a'][i - 1]; int cc = ps['c' - 'a'][j] - ps['c' - 'a'][i - 1]; r += (ca == cb || ca == 0 || cb == 0) && (cb == cc || cb == 0 || cc == 0) && (ca == cc || ca == 0 || cc == 0); } } cout << r << "\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 | #include <bits/stdc++.h> using namespace std; int ps[3][300300]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; for(int i = 0; i < s.length(); i++) { char l = s[i]; ps['a' - 'a'][i+1] = ps['a' - 'a'][i] + (l=='a'); ps['b' - 'a'][i+1] = ps['b' - 'a'][i] + (l=='b'); ps['c' - 'a'][i+1] = ps['c' - 'a'][i] + (l=='c'); } int r = 0; for(int i = 1; i <= s.length(); i++) { for(int j = i; j <= s.length(); j++) { int ca = ps['a' - 'a'][j] - ps['a' - 'a'][i - 1]; int cb = ps['b' - 'a'][j] - ps['b' - 'a'][i - 1]; int cc = ps['c' - 'a'][j] - ps['c' - 'a'][i - 1]; r += (ca == cb || ca == 0 || cb == 0) && (cb == cc || cb == 0 || cc == 0) && (ca == cc || ca == 0 || cc == 0); } } cout << r << "\n"; return 0; } |