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
#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main(){
    int mod = 1000000007;
    int n;
    cin>>n;
    
    vector<string>data(n);
    for(int i = 0; i < n; i++)  cin >> data[i];

    vector<int>temp1(601, 0);
    vector<vector<vector<int>>> dp;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            vector<vector<int>> temp2(data[i].size() + data[j].size(), temp1);
            dp.push_back(temp2);
        }
    }
    //DZIALA
    
    for(int i0 = 0; i0 < n; i0++)
    for(int j1 = 0; j1 < n; j1++){
        int i = n*i0 + j1;
        string s = data[i0] + data[j1];
        int prevR = -1;
        int prevL = -1;
        int maxPower = 0;
        
        for(int j = 0; j < s.size(); j++){
            char c = s[j];
            dp[i][j][0] = 0;
            if(c == 'P'){
                if(prevR != -1)dp[i][j][0] += dp[i][prevR][1];
                dp[i][j][0]%=mod;
                if(prevL != -1)dp[i][j][0] += dp[i][prevL][1];
                dp[i][j][0]%=mod;
            }else{
                dp[i][j][1]++;
            }
                
            
            int last = maxPower + 1;
            if(last > 600)last = 600;
            for(int k = 1; k <= last; k++){
                int sum = dp[i][j][k];
                
                int pow;
                if(c == 'L') pow = k - 1;
                else pow = k + 1;
                
                if(prevL != -1)sum += dp[i][prevL][pow];
                sum%= mod;
                if(prevR != -1)sum += dp[i][prevR][pow];
                sum%= mod;

                dp[i][j][k] = sum;
                if(k == maxPower + 1 && sum > 0)maxPower++;
            }
            if(c == 'L')prevL = j;
            else prevR = j;
        }
    }

    /*for(int i = 0; i < n; i++)
    for(int j = 0; j< n; j++){
        string s = data[i] + data[j];
        cout<<s<<endl;

        for(int k = 0; k < s.size(); k++){
            cout<<s[k]<<": ";
            for(int l = 0; l <= s.size(); l++){
                cout<<dp[i*n + j][k][l]<< " ";
            }
            cout<<endl;
        }
    }*/

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            string s = data[i] + data[j];
            int lastR = s.size() - 1;
            while(s[lastR] == 'L')lastR--;

            cout<<dp[i*n + j][lastR][0] % mod<<" ";
        }
        cout<<'\n';
    }

    /*for(int i = 0; i < n*n; i++){
        string s = data[i] + data[];
       
        if(i%n == n - 1)cout<<'\n';
    }*/

    return 0;
}