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

#include <algorithm>
#include <array>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

constexpr int MAX_M = 50;
constexpr int MAX_LENGTH = 20;

int n, m;
string target;
vector<string> candidates[MAX_LENGTH + 1];
pair<int, string> words[MAX_M];
vector<char> assign(128, -1);

int cnt;

bool dfs(int idx = 0) {
  if (idx == m) {
    if (++cnt == 1) {
      for (auto &c : target) {
        if (c != ' ')
          c = assign[c];
      }
      return true;
    } else {
      return false;
    }
  }

  const auto &s = words[idx].second;

  for (const auto &t : candidates[s.size()]) {
    auto tmp(assign);

    for (int i = 0; i < static_cast<int>(s.size()); ++i) {
      if (!(tmp[s[i]] == t[i] || (tmp[s[i]] == -1 && tmp[t[i]] == -1))) {
        goto next;
      }

      tmp[s[i]] = t[i];
      tmp[t[i]] = s[i];
    }

    assign.swap(tmp);
    if (!dfs(idx + 1))
      return false;
    assign = move(tmp);

  next:;
  }

  return true;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  while (cin >> n && n) {
    for_each(begin(candidates), end(candidates),
             [](vector<string> &v) { v.clear(); });

    for (int i = 0; i < n; ++i) {
      string in;
      cin >> in;

      const int len = in.size();
      candidates[len].emplace_back(move(in));
    }

    cin.ignore();
    getline(cin, target);
    target = target.substr(0, target.size() - 1);

    {
      string in;
      istringstream iss(target);

      for (m = 0; iss >> in; ++m) {
        words[m].second = in;
        in.erase(unique(in.begin(), in.end()), in.end());
        words[m].first = in.size();
      }
    }

    sort(words, words + m, greater<pair<int, string>>());

    cnt = 0;
    fill(begin(assign), end(assign), -1);
    dfs();

    cout << (cnt == 1 ? target : "-") << ".\\n";
  }

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

constexpr int MAX_M = 50;
constexpr int MAX_LENGTH = 20;

int n, m;
string target;
vector<string> candidates[MAX_LENGTH];
pair<int, string> words[MAX_M];
vector<char> assign(128, -1);

int cnt;

bool dfs(int idx = 0) {
  if (idx == m) {
    if (++cnt == 1) {
      for (auto &c : target) {
        if (c != ' ')
          c = assign[c];
      }
      return true;
    } else {
      return false;
    }
  }

  const auto &s = words[idx].second;

  for (const auto &t : candidates[s.size()]) {
    auto tmp(assign);

    for (int i = 0; i < static_cast<int>(s.size()); ++i) {
      if (!(tmp[s[i]] == t[i] || (tmp[s[i]] == -1 && tmp[t[i]] == -1))) {
        goto next;
      }

      tmp[s[i]] = t[i];
      tmp[t[i]] = s[i];
    }

    assign.swap(tmp);
    if (!dfs(idx + 1))
      return false;
    assign = move(tmp);

  next:;
  }

  return true;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  while (cin >> n && n) {
    for_each(begin(candidates), end(candidates),
             [](vector<string> &v) { v.clear(); });

    for (int i = 0; i < n; ++i) {
      string in;
      cin >> in;

      const int len = in.size();
      candidates[len].emplace_back(move(in));
    }

    cin.ignore();
    getline(cin, target);
    target = target.substr(0, target.size() - 1);

    {
      string in;
      istringstream iss(target);

      for (m = 0; iss >> in; ++m) {
        words[m].second = in;
        in.erase(unique(in.begin(), in.end()), in.end());
        words[m].first = in.size();
      }
    }

    sort(words, words + m, greater<pair<int, string>>());

    cnt = 0;
    fill(begin(assign), end(assign), -1);
    dfs();

    cout << (cnt == 1 ? target : "-") << ".\\n";
  }

  return EXIT_SUCCESS;
}