#include <iostream> using namespace std; string t[605]; const int base = 1024; int pref[604][604]; int lower[604][1204]; int tree[604][2051]; int naw[604]; void prepare(int a) { for (int i = 1; i <= t[a].size(); i++) { tree[a][base+i] = pref[a][i]; } tree[a][base] = base; for (int i = t[a].size()+1; i <= base; i++) tree[a][base+i] = base; for (int i = base-1; i >= 1; i--) tree[a][i] = min(tree[a][i*2], tree[a][2*i+1]); } int query(int s, int a, int b) { a += base-1; b += base+1; int ans = base; while (a/2 != b/2){ if (a%2==0) ans = min(ans, tree[s][a+1]); if (b%2) ans = min(ans, tree[s][b-1]); a/=2; b/=2; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; int maxi = 0; for (int j = 1; j <= t[i].size(); j++) { if (t[i][j-1] == 'L'){ pref[i][j] = pref[i][j-1] + 1; } else pref[i][j] = pref[i][j-1] - 1; lower[i][pref[i][j]+600]++; if (pref[i][j] > maxi) maxi = pref[i][j]+600; } for (int j = 1; j <= maxi; j++) { lower[i][j] += lower[i][j-1]; } } for (int i = 1; i <= n; i++) prepare(i); for (int i = 1; i <= n; i++) { for (int j = 1; j < t[i].size(); j++) { if (t[i][j-1] == 'L'){ for (int k = j+1; k <= t[i].size(); k+=2) { if (pref[i][j-1] == pref[i][k] && query(i, j, k) >= pref[i][j-1]) naw[i]++; } } } } for (int i = 1; i <= n; i++) { int n2 = t[i].size(); for (int j = 1; j <= n; j++) { long long ans = naw[i] + naw[j]; int min_suf = pref[i][n2]; for (int a = n2; a >= 1; a--) { if (t[i][a-1] == 'P') continue; //if (min_suf < pref[i][a-1]) {cout << a <<" "; break;} //else { for (int b = 1; b <= t[j].size(); b++) { if (pref[i][n2] + pref[j][b] == pref[i][a-1]) ans++; else if (pref[i][n2] + pref[j][b] < pref[i][a-1]) break; } if (pref[i][a-1] < min_suf) min_suf = pref[i][a-1]; //} } cout << ans << " "; } cout << "\n"; } return 0; }
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 | #include <iostream> using namespace std; string t[605]; const int base = 1024; int pref[604][604]; int lower[604][1204]; int tree[604][2051]; int naw[604]; void prepare(int a) { for (int i = 1; i <= t[a].size(); i++) { tree[a][base+i] = pref[a][i]; } tree[a][base] = base; for (int i = t[a].size()+1; i <= base; i++) tree[a][base+i] = base; for (int i = base-1; i >= 1; i--) tree[a][i] = min(tree[a][i*2], tree[a][2*i+1]); } int query(int s, int a, int b) { a += base-1; b += base+1; int ans = base; while (a/2 != b/2){ if (a%2==0) ans = min(ans, tree[s][a+1]); if (b%2) ans = min(ans, tree[s][b-1]); a/=2; b/=2; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> t[i]; int maxi = 0; for (int j = 1; j <= t[i].size(); j++) { if (t[i][j-1] == 'L'){ pref[i][j] = pref[i][j-1] + 1; } else pref[i][j] = pref[i][j-1] - 1; lower[i][pref[i][j]+600]++; if (pref[i][j] > maxi) maxi = pref[i][j]+600; } for (int j = 1; j <= maxi; j++) { lower[i][j] += lower[i][j-1]; } } for (int i = 1; i <= n; i++) prepare(i); for (int i = 1; i <= n; i++) { for (int j = 1; j < t[i].size(); j++) { if (t[i][j-1] == 'L'){ for (int k = j+1; k <= t[i].size(); k+=2) { if (pref[i][j-1] == pref[i][k] && query(i, j, k) >= pref[i][j-1]) naw[i]++; } } } } for (int i = 1; i <= n; i++) { int n2 = t[i].size(); for (int j = 1; j <= n; j++) { long long ans = naw[i] + naw[j]; int min_suf = pref[i][n2]; for (int a = n2; a >= 1; a--) { if (t[i][a-1] == 'P') continue; //if (min_suf < pref[i][a-1]) {cout << a <<" "; break;} //else { for (int b = 1; b <= t[j].size(); b++) { if (pref[i][n2] + pref[j][b] == pref[i][a-1]) ans++; else if (pref[i][n2] + pref[j][b] < pref[i][a-1]) break; } if (pref[i][a-1] < min_suf) min_suf = pref[i][a-1]; //} } cout << ans << " "; } cout << "\n"; } return 0; } |