Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf 1000000000
#define ll long long
using namespace std;
int main() {
  int len, locate, locate1, flag = 1;
  ll k, ans = 0, ans1 = 0, ans2 = 0, ans3 = 0;
  char a[105];
  scanf("%s%lld", a, &k);
  len = strlen(a);
  if (k == 1) {
    for (int i = 0; i < len; i++) {
      int tmp1 = i;
      char tmp = a[i];
      while (a[i] == tmp && i < len) {
        i++;
      }
      i--;
      ans += (i - tmp1 + 1) / 2;
    }
    printf("%lld", ans);
    return 0;
  }
  for (int i = len; i <= 2 * len - 1; i++)
    a[i] = a[i - len];
  for (int i = 0; i < 2 * len; i++) {
    int tmp1 = i;
    char tmp = a[i];
    while (a[i] == tmp && i < 2 * len) {
      i++;
    }
    i--;
    if (tmp1 <= len - 1 && i >= len) {
      locate = tmp1 - 1, locate1 = i - len + 1;
      ans1 = (i - tmp1 + 1) / 2;
      flag = 0;
      break;
    }
  }
  if (!flag) {
    if (locate == -1 && locate1 == len)
      printf("%lld", k * len / 2);
    else {
      for (int i = 0; i <= locate; i++) {
        int tmp1 = i;
        char tmp = a[i];
        while (a[i] == tmp && i <= locate) {
          i++;
        }
        i--;
        ans += (i - tmp1 + 1) / 2;
      }
      for (int i = locate1; i < len; i++) {
        int tmp1 = i;
        char tmp = a[i];
        while (a[i] == tmp && i < len) {
          i++;
        }
        i--;
        ans2 += (i - tmp1 + 1) / 2;
      }
      for (int i = locate1; i <= locate; i++) {
        int tmp1 = i;
        char tmp = a[i];
        while (a[i] == tmp && i <= locate) {
          i++;
        }
        i--;
        ans3 += (i - tmp1 + 1) / 2;
      }
      printf("%lld", ans3 * (k - 2) + ans1 * (k - 1) + ans + ans2);
    }
  } else {
    for (int i = 0; i < len; i++) {
      int tmp1 = i;
      char tmp = a[i];
      while (a[i] == tmp && i < len) {
        i++;
      }
      i--;
      ans += (i - tmp1 + 1) / 2;
    }
    printf("%lld", ans * k);
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define inf 1000000000
#define ll long long
using namespace std;
int main() {
  int len, locate, locate1, flag = 1;
  ll k, ans = 0, ans1 = 0, ans2 = 0, ans3 = 0;
  char a[205];
  scanf("%s%lld", a, &k);
  len = strlen(a);
  if (k == 1) {
    for (int i = 0; i < len; i++) {
      int tmp1 = i;
      char tmp = a[i];
      while (a[i] == tmp && i < len) {
        i++;
      }
      i--;
      ans += (i - tmp1 + 1) / 2;
    }
    printf("%lld", ans);
    return 0;
  }
  for (int i = len; i <= 2 * len - 1; i++)
    a[i] = a[i - len];
  for (int i = 0; i < 2 * len; i++) {
    int tmp1 = i;
    char tmp = a[i];
    while (a[i] == tmp && i < 2 * len) {
      i++;
    }
    i--;
    if (tmp1 <= len - 1 && i >= len) {
      locate = tmp1 - 1, locate1 = i - len + 1;
      ans1 = (i - tmp1 + 1) / 2;
      flag = 0;
      break;
    }
  }
  if (!flag) {
    if (locate == -1 && locate1 == len)
      printf("%lld", k * len / 2);
    else {
      for (int i = 0; i <= locate; i++) {
        int tmp1 = i;
        char tmp = a[i];
        while (a[i] == tmp && i <= locate) {
          i++;
        }
        i--;
        ans += (i - tmp1 + 1) / 2;
      }
      for (int i = locate1; i < len; i++) {
        int tmp1 = i;
        char tmp = a[i];
        while (a[i] == tmp && i < len) {
          i++;
        }
        i--;
        ans2 += (i - tmp1 + 1) / 2;
      }
      for (int i = locate1; i <= locate; i++) {
        int tmp1 = i;
        char tmp = a[i];
        while (a[i] == tmp && i <= locate) {
          i++;
        }
        i--;
        ans3 += (i - tmp1 + 1) / 2;
      }
      printf("%lld", ans3 * (k - 2) + ans1 * (k - 1) + ans + ans2);
    }
  } else {
    for (int i = 0; i < len; i++) {
      int tmp1 = i;
      char tmp = a[i];
      while (a[i] == tmp && i < len) {
        i++;
      }
      i--;
      ans += (i - tmp1 + 1) / 2;
    }
    printf("%lld", ans * k);
  }
  return 0;
}