Language: C++

#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unrool-loops")
#pragma GCC optimize("no-stack-limit")

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>

#define pb push_back
#define int long long
#define str string
using namespace std;
const int M = 1e9 + 7;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    str s;
    cin >> s;
    vector<vector<int>> indexes(27);
    for (int i = 0; i < n; i++) {
        indexes[s[i] - 'a'].pb(i);
    }

    vector<pair<int, int>> rebra;
    vector<int> ans(27, 1);
    vector<vector<int>> graph(27);
    for (int c = 0; c < 26; c++) {
        for (int p = c + 1; p < 26; p++) {
            if (!indexes[c].empty() && !indexes[p].empty() && indexes[c].back() > indexes[p].front()) {
                rebra.pb({p, c});
                graph[p].pb(c);
                graph[c].pb(p);
            }
        }
    }

    int color = 1;
    vector<bool> used(n, false);
    for (int i = 0; i < n; i++) {
        if (!used[i]) {
            int j = i;
            int cnt = 0;
            while (j < n) {
                cnt++;
                ans[j] = cnt;
                used[j] = true;
                int next = n;
                for (char c2 = 0; c2 < s[j] - 'a'; c2++) {
                    if (!indexes[c2].empty() && indexes[c2].back() > i) {
                        int start = upper_bound(indexes[c2].begin(), indexes[c2].end(), i) - indexes[c2].begin();
                        for (int s2 = start; s2 < indexes[c2].size(); s2++) {
                            ans[indexes[c2][s2]] = cnt + 1;
                            used[indexes[c2][s2]] = true;
                            used[indexes[c2][s2]] = true;
                        }
                        next = min(next, indexes[c2][start]);
                    }
                }
                j = next;
            }
            color = max(color, cnt);
        }
    }
    if (color > 26) {
        color = 26;
    }

    for (int _ = 0; _ < (1e7 / 26 / 26); _++) {
        for (auto p : rebra) {
            if (ans[p.first] == ans[p.second]) {
                int lol = p.first;
                if (rand() % 2 == 1) {
                    lol = p.second;
                }
                bitset<277> kek;
                for (auto l : graph[lol]) {
                    kek[ans[l]] = true;
                }
                bool f = false;
                for (int y = 1; y <= color; y++) {
                    if (!kek[y]) f = true;
                }
                if (f) {
                    for (int _ = 0; _ < 100; _++) {
                        int uu = rand() % color + 1;
                        if (!kek[uu]) {
                            ans[lol] = uu;
                            break;
                        }
                    }
                } else {
                    ans[lol] = 1 + rand() % color;
                }
            }
        }
    }
    cout << color << '\n';
    for (int i = 0; i < n; i++) cout << ans[s[i] - 'a'] << ' ';
}

Compiler output:
a.cc:4:36: warning: bad option '-funrool-loops' to pragma 'optimize' [-Wpragmas]
    4 | #pragma GCC optimize("unrool-loops")
      |                                    ^
a.cc:5:38: warning: bad option '-fno-stack-limit' to pragma 'optimize' [-Wpragmas]
    5 | #pragma GCC optimize("no-stack-limit")
      |                                      ^
a.cc:34:13: warning: bad option '-funrool-loops' to attribute 'optimize' [-Wattributes]
   34 | signed main() {
      |             ^
a.cc:34:13: warning: bad option '-fno-stack-limit' to attribute 'optimize' [-Wattributes]
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:7:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::__cxx11::basic_string<char>::_Alloc_hider::~_Alloc_hider()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = char]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/string:54:
/usr/include/c++/14/bits/basic_string.h:186:14: note: called from here
  186 |       struct _Alloc_hider : allocator_type // TODO check __is_final
      |              ^~~~~~~~~~~~