Problem p02887 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
// #define int long long
using namespace std;
inline int gi() {
  char tmp = getchar();
  int flag = 1;
  while (tmp < '0' || tmp > '9') {
    if (tmp == '-') {
      flag = -1;
      tmp = getchar();
      break;
    }
    tmp = getchar();
  }
  int ans = 0;
  while (tmp <= '9' and tmp >= '0') {
    ans = ans * 10 + tmp - '0';
    tmp = getchar();
  }
  return ans * flag;
}
inline void write(int x) {
  static int stk[100], top = 0;
  if (x == 0) {
    putchar('0');
    putchar(' ');
    return;
  }
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  while (x) {
    stk[++top] = x % 10;
    x /= 10;
  }
  while (top) {
    putchar(stk[top--] + '0');
  }
  putchar(' ');
}
#define line() putchar('\n');
#define Mem(Arr, V) memset(Arr, V, sizeof Arr);
#define Mcpy(Arr, qwq) memcpy(Arr, qwq, sizeof qwq);
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max4(max3(a, b, c), d);
#define in(a) a = gi()
#define in2(a, b) in(a), in(b)
#define in3(a, b, c) in2(a, b), in(c)
#define in4(a, b, c, d) in3(a, b, c), in(d)
#define write2(a, b) write(a), write(b)
#define write3(a, b, c) write2(a, b), write(c)
#define write4(a, b, c, d) write3(a, b, c), write(d)
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
const int inf = 2147483647; // 如果是 long long 记得改！
const int N = 1000001;
char S[N];
signed main() {
#ifndef ONLINE_JUDGE
  freopen("data.in", "r", stdin);
#endif
  int n = gi();
  char pre = 0;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    char tmp;
    scanf(" %c", &tmp);
    if (i == 1) {
      pre = tmp;
      ++ans;
    } else if (pre == tmp) {
      continue;
    } else if (pre != tmp) {
      pre = tmp;
      ++ans;
    }
  }
  write(ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
// #define int long long
using namespace std;
inline int gi() {
  char tmp = getchar();
  int flag = 1;
  while (tmp < '0' || tmp > '9') {
    if (tmp == '-') {
      flag = -1;
      tmp = getchar();
      break;
    }
    tmp = getchar();
  }
  int ans = 0;
  while (tmp <= '9' and tmp >= '0') {
    ans = ans * 10 + tmp - '0';
    tmp = getchar();
  }
  return ans * flag;
}
inline void write(int x) {
  static int stk[100], top = 0;
  if (x == 0) {
    putchar('0');
    putchar(' ');
    return;
  }
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  while (x) {
    stk[++top] = x % 10;
    x /= 10;
  }
  while (top) {
    putchar(stk[top--] + '0');
  }
  putchar(' ');
}
#define line() putchar('\n');
#define Mem(Arr, V) memset(Arr, V, sizeof Arr);
#define Mcpy(Arr, qwq) memcpy(Arr, qwq, sizeof qwq);
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max4(max3(a, b, c), d);
#define in(a) a = gi()
#define in2(a, b) in(a), in(b)
#define in3(a, b, c) in2(a, b), in(c)
#define in4(a, b, c, d) in3(a, b, c), in(d)
#define write2(a, b) write(a), write(b)
#define write3(a, b, c) write2(a, b), write(c)
#define write4(a, b, c, d) write3(a, b, c), write(d)
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
const int inf = 2147483647; // 如果是 long long 记得改！
const int N = 1000001;
char S[N];
signed main() {
  int n = gi();
  char pre = 0;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    char tmp;
    scanf(" %c", &tmp);
    if (i == 1) {
      pre = tmp;
      ++ans;
    } else if (pre == tmp) {
      continue;
    } else if (pre != tmp) {
      pre = tmp;
      ++ans;
    }
  }
  write(ans);
  return 0;
}