Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main(){
- int n; cin >> n;
- string s; cin >> s;
- vector<vector<vector<bool>>> dp(n, vector<vector<bool>>(n, vector<bool>(n, false)));
- // dp[i][j][k] = can k win in range [i, j]
- for (int i = 0; i < n; i++){
- dp[i][i][i] = true; // base cases
- }
- for (int sz = 1; sz < n; sz++) for (int l = 0; l < n - sz; l++){
- int r = l + sz;
- for (int m = l; m < r; m++){
- vector <int> wy(3, 0), wx(3, 0);
- // 0 -> fire, 1 -> water, 2 -> grass
- for (int x = l; x <= m; x++){
- if (dp[l][m][x]){
- if (s[x] == 'F') wx[0]++;
- else if (s[x] == 'W') wx[1]++;
- else wx[2]++;
- }
- }
- for (int y = m + 1; y <= r; y++){
- if (dp[m + 1][r][y]){
- if (s[y] == 'F') wy[0]++;
- else if (s[y] == 'W') wy[1]++;
- else wy[2]++;
- }
- }
- for (int x = l; x <= m; x++){
- if (dp[l][m][x]){
- if (s[x] == 'F' && (wy[0] > 0 || wy[2] > 0)) dp[l][r][x] = true; // check if there are any y's that you can beat, fire can beat fire and grass types
- if (s[x] == 'W' && (wy[1] > 0 || wy[0] > 0)) dp[l][r][x] = true;
- if (s[x] == 'G' && (wy[2] > 0 || wy[1] > 0)) dp[l][r][x] = true;
- }
- }
- for (int y = m + 1; y <= r; y++){
- if (dp[m + 1][r][y]){
- if (s[y] == 'F' && (wx[0] > 0 || wx[2] > 0)) dp[l][r][y] = true;
- if (s[y] == 'W' && (wx[1] > 0 || wx[0] > 0)) dp[l][r][y] = true;
- if (s[y] == 'G' && (wx[2] > 0 || wx[1] > 0)) dp[l][r][y] = true;
- }
- }
- }
- }
- int ans = 0;
- for (int i = 0; i < n; i++) ans += dp[0][n - 1][i]; // add 1 if i is possible winner in whole array
- cout << ans << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement