Language: C++

w#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 1; i <= (n); i ++)
#define re0(i, n) for (int i = 0; i < (int) n; i ++)
#define travel(i, u) for (int i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;

typedef long long LL;

template<typename tp> inline void read(tp &x) {
  x = 0; char c = getchar(); int f = 0;
  for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
  for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
  if (f) x = -x;
}
const int N = 1e6 + 233;
int n, ans, tot;
char s[N], t[N];
set <int> alive, rest, S[33];

inline int getfirst(int p) {
  int ch = t[p];
  auto it = S[ch].upper_bound(p);
  if (it == S[ch].begin()) {
    cout << "-1\n";
    exit(0);
  }
  return *--it;
}

inline void del(int l, int r) {
  while (true) {
    auto it = rest.lower_bound(l);
    if (it == rest.end())
      return ;
    if (*it > r)
      return ;
    S[s[*it]].erase(*it);
    rest.erase(it);
  }
}

main(void) {
  read(n); scanf("%s%s", s + 1, t + 1);
  rep (i, n) s[i] -= 'a', t[i] -= 'a';
  rep (i, n) {
    rest.insert(i);
    S[s[i]].insert(i);
  }
  rep (i, n) {
    while (i < n && t[i] == t[i + 1])
      ++ i;
    alive.insert(i);
  }
  int mx = n + 1;
  while (alive.size()) {
    auto it = alive.lower_bound(mx);
    if (it == alive.begin()) {
      mx = n + 1;
      if (tot) ++ ans;
      tot = 0;
      continue;
    }
    -- it;
    int p = *it;
    // cout << "mx = " << mx << "p = " << p << " qaq\n";
    int pre = p;
    while (p > 1 && t[p] == t[p - 1]) -- p;
    int S = getfirst(p); del(S, pre); mx = S;
    if (S == pre) {}
    else ++ tot;
    // cout << p << " " << pre << " " << tot << " xx\n";
    // cout << "S = " << S << "\n";
    alive.erase(pre);
  }
  if (tot) ++ ans;
  cout << ans << "\n";
}


Compiler output:
a.cc:1:2: error: stray '#' in program
    1 | w#include <bits/stdc++.h>
      |  ^
a.cc:1:1: error: 'w' does not name a type
    1 | w#include <bits/stdc++.h>
      | ^
a.cc: In function 'void read(tp&)':
a.cc:12:19: error: there are no arguments to 'getchar' that depend on a template parameter, so a declaration of 'getchar' must be available [-fpermissive]
   12 |   x = 0; char c = getchar(); int f = 0;
      |                   ^~~~~~~
a.cc:12:19: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
a.cc:13:49: error: there are no arguments to 'getchar' that depend on a template parameter, so a declaration of 'getchar' must be available [-fpermissive]
   13 |   for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
      |                                                 ^~~~~~~
a.cc:14:71: error: there are no arguments to 'getchar' that depend on a template parameter, so a declaration of 'getchar' must be available [-fpermissive]
   14 |   for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
      |                                                                       ^~~~~~~
a.cc: At global scope:
a.cc:20:1: error: 'set' does not name a type
   20 | set <int> alive, rest, S[33];
      | ^~~
a.cc: In function 'long long int getfirst(long long int)':
a.cc:24:13: error: 'S' was not declared in this scope
   24 |   auto it = S[ch].upper_bound(p);
      |             ^
a.cc:26:5: error: 'cout' was not declared in this scope
   26 |     cout << "-1\n";
      |     ^~~~
a.cc:27:5: error: 'exit' was not declared in this scope
   27 |     exit(0);
      |     ^~~~
a.cc:1:1: note: 'exit' is defined in header '<cstdlib>'; this is probably fixable by adding '#include <cstdlib>'
  +++ |+#include <cstdlib>
    1 | w#include <bits/stdc++.h>
a.cc: In function 'void del(long long int, long long int)':
a.cc:34:15: error: 'rest' was not declared in this scope
   34 |     auto it = rest.lower_bound(l);
      |               ^~~~
a.cc:39:5: error: 'S' was not declared in this scope
   39 |     S[s[*it]].erase(*it);
      |     ^
a.cc: At global scope:
a.cc:44:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   44 | main(void) {
      | ^~~~
a.cc: In function 'int main()':
a.cc:45:12: error: 'scanf' was not declared in this scope
   45 |   read(n); scanf("%s%s", s + 1, t + 1);
      |            ^~~~~
a.cc:48:5: error: 'rest' was not declared in this scope
   48 |     rest.insert(i);
      |     ^~~~
a.cc:49:5: error: 'S' was not declared in this scope
   49 |     S[s[i]].insert(i);
      |     ^
a.cc:54:5: error: 'alive' was not declared in this scope
   54 |     alive.insert(i);
      |     ^~~~~
a.cc:57:10: error: 'alive' was not declared in this scope
   57 |   while (alive.size()) {
      |          ^~~~~
a.cc:78:3: error: 'cout' was not declared in this scope
   78 |   cout << ans << "\n";
      |   ^~~~
a.cc: In instantiation of 'void read(tp&) [with tp = long long int]':
a.cc:45:7:   required from here
   45 |   read(n); scanf("%s%s", s + 1, t + 1);
      |   ~~~~^~~
a.cc:12:26: error: 'getchar' was not declared in this scope
   12 |   x = 0; char c = getchar(); int f = 0;
      |                   ~~~~~~~^~
a.cc:1:1: note: 'getchar' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | w#include <bits/stdc++.h>