Codeforces
CF Step
Youtube Linkedin Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Code : Pinball

#include <bits/stdc++.h>
using namespace std;

void solve(string &str) {
    int n = str.size();

    vector<int> nextl(n), prevr(n);
    nextl[n - 1] = n;
    for (int i = n - 2; i >= 0; i--) {
        if (str[i + 1] == '<') {
            nextl[i] = i + 1;
        } else {
            nextl[i] = nextl[i + 1];
        }
    }

    prevr[0] = -1;
    for (int i = 1; i < n; i++) {
        if (str[i - 1] == '>') {
            prevr[i] = i - 1;
        } else {
            prevr[i] = prevr[i - 1];
        }
    }

    for (int idx = 0; idx < n; idx++) {
        long long cost = 0;
        int now = idx, i = idx, j = idx, nxt;
        while (true) {
            if (str[now] == '>') {
                nxt = nextl[j];
                j = nxt;
            } else {
                nxt = prevr[i];
                i = nxt;
            }
            cost += abs(nxt - now);
            now = nxt;
            if (now < 0 || now >= n) {
                break;
            }
        }
        cout << cost << " ";
    }
    cout << endl;
}

int main() {
    int t;
    cin >> t;
    for (int i = 0; i < t; i++) {
        int n;
        cin >> n;
        string str;
        cin >> str;
        solve(str);
    }
    return 0;
}