Problem p03252 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define countof(a) (sizeof(a) / sizeof(*a))

#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pi>
#define pi pair<int, int>
#define fi first
#define se second
#define all(n) n.begin(), n.end()

#define FOR(var, to) for (register s64 var = 0; var < (s64)to; var++)
#define FROMTO(var, from, to)                                                  \
  for (register s64 var = from; var <= (s64)to; var++)

#define INIT(var, val) FOR(i, countof(var)) var[i] = val
#define INPUT(var) FOR(i, countof(var)) cin >> var[i]
#define INPUT1(var) FOR(i, countof(var)) cin >> var[i], var[i]--

#define SORT(v) qsort(v, countof(v), sizeof(*v), int_less)
#define SORTT(v) qsort(v, countof(v), sizeof(*v), int_greater)
#define QSORT(v, b) qsort(v, countof(v), sizeof(*v), b)

#define MOD 1000000007
#define INF ((1 << 30) - 1)
#define LINF ((1LL << 62) - 1)

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  operator int() const { return x; }
  ModInt operator=(const int p) {
    x = p;
    return ModInt(*this);
  }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }
  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};
typedef ModInt<MOD> mint;

struct UnionFind {
  vi data;
  UnionFind(int size) : data(size, -1) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool find(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
  bool united() {
    int comroot = -1;
    FOR(i, data.size()) {
      if (comroot != -1 && root(i) != comroot)
        return false;
      comroot = root(i);
    }
    return true;
  }
};

template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator+(set<Key> &a, set<Key> &b) {
  set<Key> c = a;
  for (auto i : b)
    c.insert(i);
  return c;
}
template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator+=(set<Key> &a, set<Key> &b) {
  for (auto &i : b)
    a.insert(i);
  return a;
}

template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator*(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (b.count(i))
      c.insert(i);
  return c;
}
template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator*=(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (b.count(i))
      c.insert(i);

  return a = c;
}

template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator-(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (!b.count(i))
      c.insert(i);

  return c;
}
template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator-=(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (!b.count(i))
      c.insert(i);

  return a = c;
}

static inline int ri() {
  int a;
  scanf("%d", &a);
  return a;
}

int mpow(int num, int times) {
  mint next = num;
  mint res = 1;
  while (num) {
    if (num % 2)
      res *= next;
    next *= next;
    num /= 2;
  }
  return res.x;
}

int int_less(const void *a, const void *b) {
  return (*((const int *)a) - *((const int *)b));
}
int int_greater(const void *a, const void *b) {
  return (*((const int *)b) - *((const int *)a));
}
void repl(string &s, char a, char b) {
  FOR(i, s.size()) {
    if (s[i] == a)
      s[i] = b;
    else if (s[i] == b)
      s[i] = a;
  }
}

int main() {
  string s;
  string t;
  cin >> s;
  cin >> t;
  FOR(i, s.size()) {
    if (s[i] != t[i]) {
      char c1 = s[i];
      char c2 = t[i];
      // cout << c1 << c2 << endl;
      repl(s, c1, c2);
    }
  }
  cout << (s == t ? "Yes" : "No") << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define countof(a) (sizeof(a) / sizeof(*a))

#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pi>
#define pi pair<int, int>
#define fi first
#define se second
#define all(n) n.begin(), n.end()

#define FOR(var, to) for (register s64 var = 0; var < (s64)to; var++)
#define FROMTO(var, from, to)                                                  \
  for (register s64 var = from; var <= (s64)to; var++)

#define INIT(var, val) FOR(i, countof(var)) var[i] = val
#define INPUT(var) FOR(i, countof(var)) cin >> var[i]
#define INPUT1(var) FOR(i, countof(var)) cin >> var[i], var[i]--

#define SORT(v) qsort(v, countof(v), sizeof(*v), int_less)
#define SORTT(v) qsort(v, countof(v), sizeof(*v), int_greater)
#define QSORT(v, b) qsort(v, countof(v), sizeof(*v), b)

#define MOD 1000000007
#define INF ((1 << 30) - 1)
#define LINF ((1LL << 62) - 1)

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

template <int mod> struct ModInt {
  int x;
  ModInt() : x(0) {}
  ModInt(long long y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
  ModInt &operator+=(const ModInt &p) {
    if ((x += p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((x += mod - p.x) >= mod)
      x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    x = (int)(1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-x); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return x == p.x; }
  bool operator!=(const ModInt &p) const { return x != p.x; }
  operator int() const { return x; }
  ModInt operator=(const int p) {
    x = p;
    return ModInt(*this);
  }
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    return ModInt(u);
  }
  friend ostream &operator<<(ostream &os, const ModInt<mod> &p) {
    return os << p.x;
  }
  friend istream &operator>>(istream &is, ModInt<mod> &a) {
    long long x;
    is >> x;
    a = ModInt<mod>(x);
    return (is);
  }
};
typedef ModInt<MOD> mint;

struct UnionFind {
  vi data;
  UnionFind(int size) : data(size, -1) {}
  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  bool find(int x, int y) { return root(x) == root(y); }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
  bool united() {
    int comroot = -1;
    FOR(i, data.size()) {
      if (comroot != -1 && root(i) != comroot)
        return false;
      comroot = root(i);
    }
    return true;
  }
};

template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator+(set<Key> &a, set<Key> &b) {
  set<Key> c = a;
  for (auto i : b)
    c.insert(i);
  return c;
}
template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator+=(set<Key> &a, set<Key> &b) {
  for (auto &i : b)
    a.insert(i);
  return a;
}

template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator*(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (b.count(i))
      c.insert(i);
  return c;
}
template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator*=(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (b.count(i))
      c.insert(i);

  return a = c;
}

template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator-(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (!b.count(i))
      c.insert(i);

  return c;
}
template <class Key, class Compare = less<Key>,
          class Allocator = allocator<Key>>
set<Key> operator-=(set<Key> &a, set<Key> &b) {
  set<Key> c;
  for (auto &i : a)
    if (!b.count(i))
      c.insert(i);

  return a = c;
}

static inline int ri() {
  int a;
  scanf("%d", &a);
  return a;
}

int mpow(int num, int times) {
  mint next = num;
  mint res = 1;
  while (num) {
    if (num % 2)
      res *= next;
    next *= next;
    num /= 2;
  }
  return res.x;
}

int int_less(const void *a, const void *b) {
  return (*((const int *)a) - *((const int *)b));
}
int int_greater(const void *a, const void *b) {
  return (*((const int *)b) - *((const int *)a));
}
void repl(string &s, char a, char b) {
  FOR(i, s.size()) {
    if (s[i] == a)
      s[i] = b;
    else if (s[i] == b)
      s[i] = a;
  }
}

int main() {
  string s;
  string t;
  cin >> s;
  cin >> t;
  int n = (int)s.size();
  int res = true;
  int lasts[26];
  FOR(i, 26) {
    char last = 0;
    FOR(j, n) {
      if (s[j] == 'a' + i) {
        if (!last)
          last = t[j];
        else if (t[j] != last) {
          res = false;
          break;
        }
      }
    }
    lasts[i] = last;
    FOR(j, i) if (lasts[j] && lasts[j] == last) res = false;
    if (!res)
      break;
  }
  cout << (res ? "Yes" : "No") << endl;

  return 0;
}