Problem p02891 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
ll NUM = 1e9 + 7;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 1e18
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define zer ll(0)

int main() {
  fast_cin();
  string s;
  cin >> s;
  ll k;
  cin >> k;
  ll init = 0;
  ll cnt1 = 0, cnt2 = 0;
  while (init < s.size()) {
    if (s[0] == s[init]) {
      init++;
      cnt1++;
    } else {
      break;
    }
  }
  bool flg = 0;
  if (init == s.size()) {
    flg = 1;
  }
  ll end = s.size() - 1;
  while (end >= 0) {
    if (s[s.size() - 1] == s[end]) {
      end--;
      cnt2++;
    } else {
      break;
    }
  }
  if (flg) {
    cout << k * s.size() / 2 << '\n';
  } else {
    if (s[0] == s[s.size() - 1]) {
      ll ans = 0;
      ll i = init;
      while (i <= end) {
        ll j = i;
        ll cnt = 0;
        while (j < s.size() && s[j] == s[init]) {
          j++;
          cnt++;
        }
        ans += cnt / 2;
        i = j;
      }
      ans *= k;
      ans += cnt1 / 2;
      ans += cnt2 / 2;
      ans += (k - 1) * ((cnt1 + cnt2) / 2);
      cout << ans << '\n';
    } else {
      init = 0;
      ll ans = 0;
      while (init < s.size()) {
        ll j = init;
        ll cnt = 0;
        while (j < s.size() && s[j] == s[init]) {
          j++;
          cnt++;
        }
        ans += cnt / 2;
        init = j;
      }
      cout << ans * k << '\n';
    }
  }
  return 0;
}


Fixed submission:
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int>> vv32;
typedef vector<vector<ll>> vv64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 998244353;
ll NUM = 1e9 + 7;
#define forn(i, e) for (ll i = 0; i < e; i++)
#define forsn(i, s, e) for (ll i = s; i < e; i++)
#define rforn(i, s) for (ll i = s; i >= 0; i--)
#define rforsn(i, s, e) for (ll i = s; i >= e; i--)
#define ln "\n"
#define dbg(x) cout << #x << " = " << x << ln
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define INF 1e18
#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define zer ll(0)

int main() {
  fast_cin();
  string s;
  cin >> s;
  ll k;
  cin >> k;
  ll init = 0;
  ll cnt1 = 0, cnt2 = 0;
  while (init < s.size()) {
    if (s[0] == s[init]) {
      init++;
      cnt1++;
    } else {
      break;
    }
  }
  bool flg = 0;
  if (init == s.size()) {
    flg = 1;
  }
  ll end = s.size() - 1;
  while (end >= 0) {
    if (s[s.size() - 1] == s[end]) {
      end--;
      cnt2++;
    } else {
      break;
    }
  }
  if (flg) {
    cout << k * s.size() / 2 << '\n';
  } else {
    if (s[0] == s[s.size() - 1]) {
      ll ans = 0;
      ll i = init;
      while (i <= end) {
        ll j = i;
        ll cnt = 0;
        while (j < s.size() && s[j] == s[i]) {
          j++;
          cnt++;
        }
        ans += cnt / 2;
        i = j;
      }
      ans *= k;
      ans += cnt1 / 2;
      ans += cnt2 / 2;
      ans += (k - 1) * ((cnt1 + cnt2) / 2);
      cout << ans << '\n';
    } else {
      init = 0;
      ll ans = 0;
      while (init < s.size()) {
        ll j = init;
        ll cnt = 0;
        while (j < s.size() && s[j] == s[init]) {
          j++;
          cnt++;
        }
        ans += cnt / 2;
        init = j;
      }
      cout << ans * k << '\n';
    }
  }
  return 0;
}