Skip to content

Instantly share code, notes, and snippets.

@satylogin
Created April 5, 2018 11:22
Show Gist options
  • Save satylogin/612d4f6cd2a58749906f072fc513bd86 to your computer and use it in GitHub Desktop.
Save satylogin/612d4f6cd2a58749906f072fc513bd86 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define ll long long int
#define ld long double
#define pb push_back
#define mkp make_pair
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define fi first
#define sc second
#define eps 1e-9
vector<pair<char, int> > v;
string s;
int main()
{
ios_base::sync_with_stdio(false);cout.tie(0);cin.tie(0);
int n, m, i, j, k, x, y, z;
cin >> s;
ll ans = 0;
char c = s[0];
int freq = 1;
for (i = 1; s[i]; ++i) {
if (s[i] != s[i-1]) {
v.pb(mkp(c, freq));
c = s[i];
freq = 0;
}
freq++;
}
if (freq > 0) v.pb(mkp(c, freq));
for (i = 0; i < v.size(); ++i) {
ans += (1ll * v[i].sc * (v[i].sc + 1ll) / 2);
if ((i+2 < v.size()) && (v[i].fi == v[i+2].fi) && (v[i+1].sc == 1)) {
ans += min(v[i].sc, v[i+2].sc);
}
}
cout << ans << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment