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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <cstdio>
#include <cstring>
#include <unordered_set>
#include <algorithm>

#define MAX_N 1000
#define MOD 1000000000000000003LL

using namespace std; 

char dribles[MAX_N][MAX_N];
int lens[MAX_N];

char input[2 * MAX_N];
int l, n;

unordered_set<long long> hashes;

// Hash function 
struct hashFunction
{
  size_t operator()(const pair<int, long long> &x) const
  {
    return x.first ^ x.second;
  }
};

unordered_set<pair<int, long long>, hashFunction > beenHere;

long long fmod(long long n) {
    if (n > MOD) return n - MOD;
    return n;
}

void dfs(int pos, long long hash, int parDiff) {
    //printf("DFS %d %lld %d\n", pos, hash, parDiff);
    if (parDiff < 0) return;
    if (beenHere.find(make_pair(pos, hash)) != beenHere.end()) {
        //printf("BEEN HERE\n");
        return;
    }
    beenHere.insert(make_pair(pos, hash));
    if (pos == l) {
        if (parDiff != 0) return;
        if (hashes.find(hash) != hashes.end()) return;
        hashes.insert(hash);
    } else {
        dfs(pos + 1, hash, parDiff);
        if (input[pos] == 'L') {
            dfs(pos + 1, fmod(hash * 3), parDiff + 1);
        } else {
            dfs(pos + 1, fmod(hash * 3 + 1), parDiff - 1);
        }
    }
}


int solve() {
    hashes.clear();
    beenHere.clear();
    dfs(0, 37, 0);
    return hashes.size();
}

int main() {
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", dribles[i]);
        lens[i] = strlen(dribles[i]);
    }

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            memcpy(input, dribles[i], lens[i]);
            memcpy(input + lens[i], dribles[j], lens[j]);
            l = lens[i] + lens[j];
            input[l] = 0;
            
            //printf("\n%s .    ", input);
            printf("%d ", solve() - 1);
        }
        printf("\n");
    }
}

/*
4
LLPLPP
PPLP
LLP
P
*/