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
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>

#define MAX_STR 601
#define MOD 1000000007

using namespace std;

struct dribbling
{
    char seq[MAX_STR];
    unsigned long long ll[MAX_STR];
    unsigned long long lp[MAX_STR];
    int length;
};

void calulateCoefficientsForDribbling(dribbling &dribbling)
{
    dribbling.ll[0] = 1;
    dribbling.lp[0] = 0;

    for (int i = 1; i < MAX_STR; ++i)
    {
        dribbling.ll[i] = 0;
        dribbling.lp[i] = 0;
    }


    int i = 0;
    while (dribbling.seq[i] != '\0')
    {
        switch (dribbling.seq[i])
        {
        case 'L':
            for (int j = MAX_STR - 1; j > 0; --j)
            {
                dribbling.ll[j] = (dribbling.lp[j - 1] + dribbling.ll[j - 1]) % MOD;
                // lp[j] = lp[j];
            }
            break;

        case 'P':
            for (int j = 0; j < MAX_STR - 1; ++j)
            {
                // ll[j] = ll[j];
                dribbling.lp[j] = (dribbling.ll[j + 1] + dribbling.lp[j + 1]) % MOD;
            }
            break;
        }

        ++i;
    }
    dribbling.length = i;
}

void calulateCoefficientsForReversedDribbling(dribbling &dribbling)
{
    dribbling.ll[0] = 0;
    dribbling.lp[0] = 0;

    for (int i = 1; i < MAX_STR; ++i)
    {
        dribbling.ll[i] = 0;
        dribbling.lp[i] = 0;
    }

    switch (dribbling.seq[0])
    {
    case 'L':
        dribbling.lp[0] = 1;
        break;
    case 'P':
        dribbling.ll[1] = dribbling.lp[1] = 1;
        break;
    }

    int i = 1;
    while (dribbling.seq[i] != '\0')
    {
        switch (dribbling.seq[i])
        {
        case 'L':
            dribbling.ll[0] = (dribbling.ll[0] + dribbling.ll[1]) % MOD;
            dribbling.lp[0] = (dribbling.lp[0] + dribbling.ll[1]) % MOD;
            for (int j = 1; j < MAX_STR - 1; ++j)
            {
                dribbling.ll[j] = dribbling.ll[j + 1];
                dribbling.lp[j] = (dribbling.lp[j] + dribbling.ll[j + 1]) % MOD;
            }
            dribbling.ll[MAX_STR - 1] = 0;
            break;

        case 'P':
            for (int j = MAX_STR - 1; j > 0; --j)
            {
                dribbling.ll[j] = (dribbling.ll[j] + dribbling.lp[j - 1]) % MOD;
                dribbling.lp[j] = dribbling.lp[j - 1];
            }
            dribbling.lp[0] = 0;
            break;
        }

        ++i;
    }
}

void reverseDribbling(const dribbling &d, dribbling &rev)
{
    rev.length = d.length;
    rev.seq[d.length] = '\0';
    for (int i = 0; i < d.length; ++i)
    {
        rev.seq[d.length - i - 1] = d.seq[i];
    }
}

unsigned long long getFantasticDribblingsForPairNormalReversed(const dribbling &d, const dribbling &rev)
{
    unsigned long long result = 0;
    for (int i = 0; i < MAX_STR; ++i)
    {
        result = (result + ((d.ll[i] * rev.ll[i]) % MOD) + ((d.lp[i] * rev.lp[i]) % MOD)) % MOD;
    }
    return result;
}

int main()
{
    int n;
    cin >> n;

    dribbling *data = new dribbling[n];
    dribbling *rev_data = new dribbling[n];

    for (int i = 0; i < n; ++i)
    {
        cin >> data[i].seq;
        calulateCoefficientsForDribbling(data[i]);

        reverseDribbling(data[i], rev_data[i]);

        calulateCoefficientsForReversedDribbling(rev_data[i]);
    }

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            cout << getFantasticDribblingsForPairNormalReversed(data[i], rev_data[j]) << " ";
        }
        cout << "\n";
    }

    delete[] data;
    delete[] rev_data;
    return 0;
}