#include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define st first #define nd second using namespace std; int n, vis[1205][1205], nextl[1205], nextr[1205], mod = 1e9 + 7, lastl, lastr, ileusun; string S[603], s; pair<int, int> usun[5000000]; void wyczysc(){ while(ileusun > 0){ vis[usun[ileusun].st][usun[ileusun].nd] = -1; ileusun--; } } int ile(int ost, int x){ if(ost != -1 && vis[ost][x] != -1) return vis[ost][x]; int res = 0; if(x == 0) res = 1; int l, r; l = nextl[ost + 1]; if(l < s.size()) res = (res + ile(l, x + 1)) % mod; r = nextr[ost + 1]; if(r < s.size() && x > 0) res = (res + ile(r, x - 1)) % mod; if(ost != -1){ vis[ost][x] = res; ileusun++; usun[ileusun] = {ost, x}; } return res; } int main(){ iamspeed; cin >> n; for(int i = 1 ; i <= n ; i++){ cin >> S[i]; } for(int i = 0 ; i <= 1202 ; i++){ for(int j = 0 ; j <= 1202 ; j++){ vis[i][j] = -1; } } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ s = ""; s += S[i]; s += S[j]; lastl = lastr = s.size(); nextl[s.size()] = nextr[s.size()] = s.size(); for(int k = s.size() - 1 ; k >= 0 ; k--){ if(s[k] == 'L') lastl = k; else if(s[k] == 'P') lastr = k; nextl[k] = lastl; nextr[k] = lastr; } wyczysc(); ileusun = 0; cout << ile(-1, 0) - 1 << " "; } cout << endl; } }
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 | #include <bits/stdc++.h> #define iamspeed ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define st first #define nd second using namespace std; int n, vis[1205][1205], nextl[1205], nextr[1205], mod = 1e9 + 7, lastl, lastr, ileusun; string S[603], s; pair<int, int> usun[5000000]; void wyczysc(){ while(ileusun > 0){ vis[usun[ileusun].st][usun[ileusun].nd] = -1; ileusun--; } } int ile(int ost, int x){ if(ost != -1 && vis[ost][x] != -1) return vis[ost][x]; int res = 0; if(x == 0) res = 1; int l, r; l = nextl[ost + 1]; if(l < s.size()) res = (res + ile(l, x + 1)) % mod; r = nextr[ost + 1]; if(r < s.size() && x > 0) res = (res + ile(r, x - 1)) % mod; if(ost != -1){ vis[ost][x] = res; ileusun++; usun[ileusun] = {ost, x}; } return res; } int main(){ iamspeed; cin >> n; for(int i = 1 ; i <= n ; i++){ cin >> S[i]; } for(int i = 0 ; i <= 1202 ; i++){ for(int j = 0 ; j <= 1202 ; j++){ vis[i][j] = -1; } } for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ s = ""; s += S[i]; s += S[j]; lastl = lastr = s.size(); nextl[s.size()] = nextr[s.size()] = s.size(); for(int k = s.size() - 1 ; k >= 0 ; k--){ if(s[k] == 'L') lastl = k; else if(s[k] == 'P') lastr = k; nextl[k] = lastl; nextr[k] = lastr; } wyczysc(); ileusun = 0; cout << ile(-1, 0) - 1 << " "; } cout << endl; } } |