Problem p03069 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <cmath>
using namespace std;

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x)*1ll * (x))
#define all(x) x.begin(), x.end()
#define clr(a, v) memset(a, v, sizeof(a))

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)
#define reflex ios_base::sync_with_stdio(false)

#define nl '\n'
#define ioi exit(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = (int)2e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;

const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int kx[] = {-1, 1, 2, 2, 1, -1, -2, -2};
const int ky[] = {2, 2, 1, -1, -2, -2, -1, 1};

string conv(int a) {
  stringstream ss;
  ss << a;
  string str = ss.str();
  return str;
}

string conv(char a) {
  stringstream ss;
  ss << a;
  string str = ss.str();
  return str;
}

int conv(string a) {
  stringstream geek(a);
  int x = 0;
  geek >> x;
  return x;
}

string cad;
char s[5000];

int memoA[N];
int memoB[N];

int B(int i) {
  int &m = memoB[i];
  if (m != -1)
    return m;
  if (i == 0)
    return 0;
  if (s[i] == '.') {
    m = B(i - 1);
    return m;
  } else {
    m = B(i - 1) + 1;
    return m;
  }
}

int A(int i) {
  int &m = memoA[i];
  if (m != -1)
    return m;
  if (i == 0)
    return inf;
  if (s[i] == '.') {
    m = min(A(i - 1), B(i - 1)) + 1;
    ;
    return m;
  } else {
    m = min(A(i - 1), B(i - 1));
    return m;
  }
}

int main(void) {

  clr(memoA, -1);
  clr(memoB, -1);

  int x;
  cin >> x;
  cin >> cad;
  int n = (int)cad.size();
  rep(i, 0, n - 1) s[i + 1] = cad[i];

  // cout<< A(n) <<" "<< B(n)<<endl;
  cout << min(A(n), B(n));
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <cmath>
using namespace std;

#define f first
#define s second

#define pb push_back
#define pp pop_back
#define mp make_pair

#define sz(x) (int)x.size()
#define sqr(x) ((x)*1ll * (x))
#define all(x) x.begin(), x.end()
#define clr(a, v) memset(a, v, sizeof(a))

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, l, r) for (int i = (l); i >= (r); i--)
#define reflex ios_base::sync_with_stdio(false)

#define nl '\n'
#define ioi exit(0);

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = (int)2e5 + 7;
const int inf = (int)1e9 + 7;
const int mod = (int)1e9 + 7;
const ll linf = (ll)1e18 + 7;

const int dx[] = {-1, 0, 1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int kx[] = {-1, 1, 2, 2, 1, -1, -2, -2};
const int ky[] = {2, 2, 1, -1, -2, -2, -1, 1};

string conv(int a) {
  stringstream ss;
  ss << a;
  string str = ss.str();
  return str;
}

string conv(char a) {
  stringstream ss;
  ss << a;
  string str = ss.str();
  return str;
}

int conv(string a) {
  stringstream geek(a);
  int x = 0;
  geek >> x;
  return x;
}

string cad;
char s[N];

int memoA[N];
int memoB[N];

int B(int i) {
  int &m = memoB[i];
  if (m != -1)
    return m;
  if (i == 0)
    return 0;
  if (s[i] == '.') {
    m = B(i - 1);
    return m;
  } else {
    m = B(i - 1) + 1;
    return m;
  }
}

int A(int i) {
  int &m = memoA[i];
  if (m != -1)
    return m;
  if (i == 0)
    return inf;
  if (s[i] == '.') {
    m = min(A(i - 1), B(i - 1)) + 1;
    ;
    return m;
  } else {
    m = min(A(i - 1), B(i - 1));
    return m;
  }
}

int main(void) {

  clr(memoA, -1);
  clr(memoB, -1);

  int x;
  cin >> x;
  cin >> cad;
  int n = (int)cad.size();
  rep(i, 0, n - 1) s[i + 1] = cad[i];

  // cout<< A(n) <<" "<< B(n)<<endl;
  cout << min(A(n), B(n));
  return 0;
}