#include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 601; vi vec[maxn]; vi nast[maxn][2]; vi poprz[maxn][2]; // 0 - L int tylko[maxn]; int blokada[maxn][maxn][2]; int whole[maxn][maxn][2]; // ile jest ciagow sufiksowych konczacych sie na cos const int mod = 1e9+7; inline void addmod(int &x, int val) { x += val; while (x > mod) x -= mod; } int get_result(int a, int b) { int result = tylko[a]; int k = min(sz(vec[a]), sz(vec[b])) - 1; rep(bit, 0, 2) { for (int delta = 0; delta <= k; ++delta) { addmod(result, ((ll)blokada[a][delta][bit] * whole[b][delta][bit]) % mod); } } return result; } int sufdp[maxn][maxn][2]; int prefdp[maxn][maxn]; bool TESTRUN = false; int main() { boost; int n; cin >> n; FOR(i, 1, n) { string s; if (!TESTRUN) cin >> s; else { FOR(j, 1, 600) { int bit = rand()%2; if (bit) s += 'L'; else s += 'P'; } } int k = sz(s); vec[i].resize(k + 1); rep(j, 0, k) vec[i][j + 1] = (s[j] == 'L') ? 0 : 1; nast[i][0].resize(k + 2); nast[i][1].resize(k + 2); nast[i][0][k + 1] = -1; nast[i][1][k + 1] = -1; for (int j = k; j > 0; --j) { rep(bit, 0, 2) nast[i][bit][j] = nast[i][bit][j + 1]; nast[i][vec[i][j]][j] = j; } poprz[i][0].resize(k + 1); poprz[i][1].resize(k + 1); poprz[i][0][0] = -1; poprz[i][1][0] = -1; for (int j = 1; j <= k; ++j) { rep(bit, 0, 2) poprz[i][bit][j] = poprz[i][bit][j - 1]; poprz[i][vec[i][j]][j] = j; } } FOR(i, 1, n) { int k = sz(vec[i]) - 1; // dp[prefiks][delta > 0] rep(a, 0, k+1) rep(b, 0, k+1) prefdp[a][b] = 0; prefdp[0][0] = 1; FOR(len, 0, k) { FOR(delta, 0, len) { rep(bit, 0, 2) { int gdzie = nast[i][bit][len + 1]; if (gdzie == -1) { addmod(blokada[i][delta][bit], prefdp[len][delta]); continue; } if (bit == 0) addmod(prefdp[gdzie][delta + 1], prefdp[len][delta]); else if (delta > 0) addmod(prefdp[gdzie][delta - 1], prefdp[len][delta]); } } } // dp[sufiks][delta > 0 na odwrot][ostatni znak zeby uzyskac] rep(a, 0, k+2) rep(b, 0, k + 2) sufdp[a][b][0] = sufdp[a][b][1] = 0; sufdp[k + 1][0][0] = 1; //potentially sketchy? for (int len = k + 1; len > 0; --len) { int max_delta = k + 1 - len; FOR(delta, 0, max_delta) { rep(bit, 0, 2) { int total = sufdp[len][delta][0]; addmod(total, sufdp[len][delta][1]); if (len != k + 1) addmod(whole[i][delta][bit], sufdp[len][delta][bit]); int gdzie = poprz[i][bit][len - 1]; if (gdzie == -1) { continue; } if (bit == 1) addmod(sufdp[gdzie][delta + 1][bit], total); else if (delta > 0) addmod(sufdp[gdzie][delta - 1][bit], total); } } } tylko[i] = 0; FOR(len, 1, k) addmod(tylko[i], prefdp[len][0]); } FOR(i, 1, n) { FOR(j, 1, n) cout << get_result(i, j) << ' '; cout << "\n"; } }
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 157 158 159 160 | #include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define OUT(x) {cout << x << "\n"; exit(0); } #define TCOUT(x) {cout << x << "\n"; return; } #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define rep(i, l, r) for(int i = (l); i < (r); ++i) #define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } #define sz(x) int(x.size()) #define trav(a, x) for(auto& a : x) #define all(x) begin(x), end(x) typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; #ifdef DEBUG template<class T> int size(T &&x) { return int(x.size()); } template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) { return out << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) { out << '{'; for(auto it = x.begin(); it != x.end(); ++it) out << *it << (it == prev(x.end()) ? "" : ", "); return out << '}'; } void dump() {} template<class T, class... Args> void dump(T &&x, Args... args) { cerr << x << "; "; dump(args...); } #endif #ifdef DEBUG struct Nl{~Nl(){cerr << '\n';}}; # define debug(x...) cerr << (strcmp(#x, "") ? #x ": " : ""), dump(x), Nl(), cerr << "" #else # define debug(...) 0 && cerr #endif const int maxn = 601; vi vec[maxn]; vi nast[maxn][2]; vi poprz[maxn][2]; // 0 - L int tylko[maxn]; int blokada[maxn][maxn][2]; int whole[maxn][maxn][2]; // ile jest ciagow sufiksowych konczacych sie na cos const int mod = 1e9+7; inline void addmod(int &x, int val) { x += val; while (x > mod) x -= mod; } int get_result(int a, int b) { int result = tylko[a]; int k = min(sz(vec[a]), sz(vec[b])) - 1; rep(bit, 0, 2) { for (int delta = 0; delta <= k; ++delta) { addmod(result, ((ll)blokada[a][delta][bit] * whole[b][delta][bit]) % mod); } } return result; } int sufdp[maxn][maxn][2]; int prefdp[maxn][maxn]; bool TESTRUN = false; int main() { boost; int n; cin >> n; FOR(i, 1, n) { string s; if (!TESTRUN) cin >> s; else { FOR(j, 1, 600) { int bit = rand()%2; if (bit) s += 'L'; else s += 'P'; } } int k = sz(s); vec[i].resize(k + 1); rep(j, 0, k) vec[i][j + 1] = (s[j] == 'L') ? 0 : 1; nast[i][0].resize(k + 2); nast[i][1].resize(k + 2); nast[i][0][k + 1] = -1; nast[i][1][k + 1] = -1; for (int j = k; j > 0; --j) { rep(bit, 0, 2) nast[i][bit][j] = nast[i][bit][j + 1]; nast[i][vec[i][j]][j] = j; } poprz[i][0].resize(k + 1); poprz[i][1].resize(k + 1); poprz[i][0][0] = -1; poprz[i][1][0] = -1; for (int j = 1; j <= k; ++j) { rep(bit, 0, 2) poprz[i][bit][j] = poprz[i][bit][j - 1]; poprz[i][vec[i][j]][j] = j; } } FOR(i, 1, n) { int k = sz(vec[i]) - 1; // dp[prefiks][delta > 0] rep(a, 0, k+1) rep(b, 0, k+1) prefdp[a][b] = 0; prefdp[0][0] = 1; FOR(len, 0, k) { FOR(delta, 0, len) { rep(bit, 0, 2) { int gdzie = nast[i][bit][len + 1]; if (gdzie == -1) { addmod(blokada[i][delta][bit], prefdp[len][delta]); continue; } if (bit == 0) addmod(prefdp[gdzie][delta + 1], prefdp[len][delta]); else if (delta > 0) addmod(prefdp[gdzie][delta - 1], prefdp[len][delta]); } } } // dp[sufiks][delta > 0 na odwrot][ostatni znak zeby uzyskac] rep(a, 0, k+2) rep(b, 0, k + 2) sufdp[a][b][0] = sufdp[a][b][1] = 0; sufdp[k + 1][0][0] = 1; //potentially sketchy? for (int len = k + 1; len > 0; --len) { int max_delta = k + 1 - len; FOR(delta, 0, max_delta) { rep(bit, 0, 2) { int total = sufdp[len][delta][0]; addmod(total, sufdp[len][delta][1]); if (len != k + 1) addmod(whole[i][delta][bit], sufdp[len][delta][bit]); int gdzie = poprz[i][bit][len - 1]; if (gdzie == -1) { continue; } if (bit == 1) addmod(sufdp[gdzie][delta + 1][bit], total); else if (delta > 0) addmod(sufdp[gdzie][delta - 1][bit], total); } } } tylko[i] = 0; FOR(len, 1, k) addmod(tylko[i], prefdp[len][0]); } FOR(i, 1, n) { FOR(j, 1, n) cout << get_result(i, j) << ' '; cout << "\n"; } } |