Problem p03252 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x) for (__typeof(x) i = 0; i < x; i++)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scii(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%d %d", &x, &y);
#define sciii(x, y, z)                                                         \
  int x, y, z;                                                                 \
  scanf("%d %d %d", &x, &y, &z);
#define TC(x)                                                                  \
  sci(x);                                                                      \
  while (x--)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define debug(x)                                                               \
  { cerr << #x << " = " << x << endl; }
#define repi(i, x) for (__typeof(x) i = x - 1; i >= 0; i--)
#define fore(itr, x)                                                           \
  for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x)                                                          \
  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vector<int>> vvi;
const int infi = numeric_limits<int>::max();
const double eps = 0;
const int ms = 0;

bool func(const vi &a, const vi &b) {
  rep(k, a.size()) {
    if (a[k] != b[k])
      return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false); // endl->"\n"
  cin.tie(0);

  string s, t;
  cin >> s >> t;

  vvi a(26, vi(0)), b(26, vi(0));
  int n = s.size();

  rep(i, n) {
    a[s[i] - 'a'].pb(i);
    b[t[i] - 'a'].pb(i);
  }

  // rep(j, 26)debug(a[j].size())

  rep(i, 26) {
    bool f = false;
    if (a[i].size() == 0)
      continue;
    rep(j, 26) {
      if (b[j].size() == 0 || a[i].size() != b[j].size())
        continue;
      if (func(a[i], a[j])) {
        f = true;
        break;
      }
    }

    if (!f) {
      cout << "No"
           << "\n";
      return 0;
    }
  }
  cout << "Yes"
       << "\n";
  return 0;
}

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, x) for (__typeof(x) i = 0; i < x; i++)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define sci(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x);
#define scii(x, y)                                                             \
  int x, y;                                                                    \
  scanf("%d %d", &x, &y);
#define sciii(x, y, z)                                                         \
  int x, y, z;                                                                 \
  scanf("%d %d %d", &x, &y, &z);
#define TC(x)                                                                  \
  sci(x);                                                                      \
  while (x--)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define debug(x)                                                               \
  { cerr << #x << " = " << x << endl; }
#define repi(i, x) for (__typeof(x) i = x - 1; i >= 0; i--)
#define fore(itr, x)                                                           \
  for (__typeof(x.begin()) itr = x.begin(); itr != x.end(); itr++)
#define forei(itr, x)                                                          \
  for (__typeof(x.end()) itr = x.end() - 1; itr != x.begin() - 1; itr--)
typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vector<int>> vvi;
const int infi = numeric_limits<int>::max();
const double eps = 0;
const int ms = 0;

bool func(const vi &a, const vi &b) {
  rep(k, a.size()) {
    if (a[k] != b[k])
      return false;
  }
  return true;
}

int main() {
  ios::sync_with_stdio(false); // endl->"\n"
  cin.tie(0);

  string s, t;
  cin >> s >> t;

  vvi a(26, vi(0)), b(26, vi(0));
  int n = s.size();

  rep(i, n) {
    a[s[i] - 'a'].pb(i);
    b[t[i] - 'a'].pb(i);
  }

  // rep(j, 26)debug(a[j].size())

  rep(i, 26) {
    bool f = false;
    if (a[i].size() == 0)
      continue;
    rep(j, 26) {
      if (b[j].size() == 0 || a[i].size() != b[j].size())
        continue;
      if (func(a[i], b[j])) {
        f = true;
        break;
      }
    }

    if (!f) {
      cout << "No"
           << "\n";
      return 0;
    }
  }
  cout << "Yes"
       << "\n";
  return 0;
}