1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <bits/stdc++.h>
using namespace std;

unordered_set<int> us;

long long xd(string s) {
    us.clear();
    int n = s.size();
    for(int b = 0; b < (1<<n); ++b) {
        bool ok = true;
        int prefiks = 0;
        int id = 0;
        for(int i = 0; i < n; ++i)
            if(b & (1 << i)) {
                if((prefiks += s[i]) < 0) {
                    ok = false; break;
                }
                id <<= 1;
                id |= (1 & (s[i] == -1));
            }
        if(!ok || prefiks != 0) continue;
        us.insert(id);
    }
    return us.size() - 1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n; cin >> n;
    vector<string> s(n);
    for(auto &&e : s) cin >> e;
    for(auto &&e : s) for(auto &&c : e) c = c == 'L' ? 1 : -1;
    for(auto &&i : s) { for(auto &&j : s) cout << xd(i+j) << ' '; cout << '\n'; }
}