Problem p01145 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef string::iterator State;

char buf[114514];
string s;

bool IsVoewl(char c) { return (c != '\\0') && (strchr("aiueo", c) != NULL); }

bool IsSoundless(string s) {
  return (s[0] != '\\0') && (strchr("ksthp", s[0]) != NULL);
}

bool IsConsonant(char c) {
  return (c != '\\0') && (strchr("kstnhmyrwgzdbp", c) != NULL);
}

vector<string> ParseSyllable(State itr) {
  vector<string> ret;

  while (itr != s.end()) {
    string t;
    if (IsVoewl(*itr)) {
      t += *itr++;
      ret.emplace_back(t);
      continue;
    }
    assert(IsConsonant(*itr));

    t += *itr++;
    if (*itr == 'y') {
      assert(t[0] != 'y' && t[0] != 'w');
      t += *itr++;
      assert(IsVoewl(*itr));
      assert(*itr == 'a' || *itr == 'u' || *itr == 'o');
      t += *itr++;
      ret.emplace_back(t);
      continue;
    }

    if (IsVoewl(*itr)) {
      if (t[0] == 'y')
        assert(*itr == 'a' || *itr == 'u' || *itr == 'o');
      if (t[0] == 'w')
        assert(*itr == 'a');
      t += *itr++;
      ret.emplace_back(t);
      continue;
    }

    if (IsConsonant(*itr)) {
      if (*itr == t[0]) {
        ret.emplace_back(t);
        continue;
      } else if (t[0] == 'n') {
        ret.emplace_back(t);
        continue;
      } else
        assert(0);
    }

    assert(itr == s.end() || *itr == '\\'');
    if (*itr == '\\'')
      itr++;
    ret.emplace_back(t);
  }

  return ret;
}

int main() {
  while (1) {
    scanf("%s", buf);
    s = buf;
    if (s[0] == '#')
      return 0;

    set<int> soundless;
    vector<string> lis = ParseSyllable(s.begin());
    bool canbe = true;
    char idx = 0;
    for (int i = 0; i < lis.size(); i++) {
      int n = lis[i].size();
      char c = lis[i][n - 1];

      idx += n;
      if (IsVoewl(c) && !canbe) {
        canbe = true;
        continue;
      }

      if (c == 'i' || c == 'u') {
        if (!IsSoundless(lis[i]))
          continue;
        if (i == lis.size() - 1 || IsSoundless(lis[i + 1])) {
          soundless.insert(idx - 1);
          canbe = false;
          continue;
        }
      } else if (c == 'a' || c == 'o') {
        if (i == lis.size() - 1)
          continue;
        if (!IsSoundless(lis[i]))
          continue;
        if (!IsSoundless(lis[i + 1]))
          continue;
        if (lis[i + 1][lis[i + 1].size() - 1] == c) {
          soundless.insert(idx - 1);
          canbe = false;
          continue;
        }
      }
    }

    int off = 0;
    for (int i = 0; i < s.size(); i++) {
      if (soundless.count(i - off)) {
        printf("(%c)", s[i]);
      } else
        printf("%c", s[i]);
      if (s[i] == '\\'')
        off++;
    }
    puts("");
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef string::iterator State;

char buf[114514];
string s;

bool IsVoewl(char c) { return (c != '\\0') && (strchr("aiueo", c) != NULL); }

bool IsSoundless(char c) { return (c != '\\0') && (strchr("ksthp", c) != NULL); }

bool IsConsonant(char c) {
  return (c != '\\0') && (strchr("kstnhmyrwgzdbp", c) != NULL);
}

vector<string> ParseSyllable(State itr) {
  vector<string> ret;

  while (itr != s.end()) {
    string t;
    if (IsVoewl(*itr)) {
      t += *itr++;
      ret.emplace_back(t);
      continue;
    }
    assert(IsConsonant(*itr));

    t += *itr++;
    if (*itr == 'y') {
      assert(t[0] != 'y' && t[0] != 'w');
      t += *itr++;
      assert(IsVoewl(*itr));
      assert(*itr == 'a' || *itr == 'u' || *itr == 'o');
      t += *itr++;
      ret.emplace_back(t);
      continue;
    }

    if (IsConsonant(*itr)) {
      assert(*itr == t[0]);
      ret.emplace_back(t);
      continue;
    }

    if (IsVoewl(*itr)) {
      if (t[0] == 'y')
        assert(*itr == 'a' || *itr == 'u' || *itr == 'o');
      if (t[0] == 'w')
        assert(*itr == 'a');
      t += *itr++;
      ret.emplace_back(t);
      continue;
    }

    assert(t[0] == 'n');
    if (*itr == '\\'')
      itr++;
    ret.emplace_back(t);
  }

  return ret;
}

int main() {
  while (1) {
    scanf("%s", buf);
    s = buf;
    if (s[0] == '#')
      return 0;

    set<int> soundless;
    vector<string> lis = ParseSyllable(s.begin());
    bool canbe = true;
    char idx = 0;
    for (int i = 0; i < lis.size(); i++) {
      int n = lis[i].size();
      char c = lis[i][n - 1];

      idx += n;
      if (IsVoewl(c) && !canbe) {
        canbe = true;
        continue;
      }

      if (c == 'i' || c == 'u') {
        if (n < 2)
          continue;
        if (!IsSoundless(lis[i][n - 2]))
          continue;
        if (i == lis.size() - 1 || IsSoundless(lis[i + 1][0])) {
          soundless.insert(idx - 1);
          canbe = false;
          continue;
        }
      } else if (c == 'a' || c == 'o') {
        if (i == lis.size() - 1)
          continue;
        if (n != 2)
          continue;
        if (!IsSoundless(lis[i][n - 2]))
          continue;
        if (lis[i + 1].size() != 2)
          continue;
        if (IsSoundless(lis[i + 1][0]) && lis[i + 1][1] == c) {
          soundless.insert(idx - 1);
          canbe = false;
          continue;
        }
      }
    }

    for (int i = 0; i < s.size(); i++) {
      if (soundless.count(i)) {
        printf("(%c)", s[i]);
      } else
        printf("%c", s[i]);
    }
    puts("");
  }
}