Problem p03231 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

ll N, M;
string S, T;
void input() {
  fast_io();
  cin >> N >> M;
  cin >> S >> T;
}

ll __gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return __gcd(y % x, x);
}
ll lcm(ll x, ll y) { return x * y / __gcd(x, y); }

int solve() {
  ll L = lcm(N, M);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (i * L / N == j * L / M) {
        if (S[i] == T[j])
          continue;
        else {
          cout << -1 << endl;
          return 0;
        }
      }
    }
  }
  cout << L << endl;

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

ll N, M;
string S, T;
void input() {
  fast_io();
  cin >> N >> M;
  cin >> S >> T;
}

ll __gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return __gcd(y % x, x);
}
ll lcm(ll x, ll y) { return x * y / __gcd(x, y); }

int solve() {
  ll L = lcm(N, M);
  for (int i = 0; i < N; i++) {
    int j = i * (L / N) / (L / M);
    if (i * L / N == j * L / M) {
      if (S[i] == T[j])
        continue;
      else {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  cout << L << endl;

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}