Advertisement
shreyanray

Untitled

Apr 25th, 2024
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main(){
  5.     int n; cin >> n;
  6.     string s; cin >> s;
  7.    
  8.     vector<vector<vector<bool>>> dp(n, vector<vector<bool>>(n, vector<bool>(n, false)));
  9.     // dp[i][j][k] = can k win in range [i, j]
  10.    
  11.     for (int i = 0; i < n; i++){
  12.         dp[i][i][i] = true; // base cases
  13.     }
  14.    
  15.     for (int sz = 1; sz < n; sz++) for (int l = 0; l < n - sz; l++){
  16.         int r = l + sz;
  17.        
  18.         for (int m = l; m < r; m++){
  19.             vector <int> wy(3, 0), wx(3, 0);
  20.            
  21.             // 0 -> fire, 1 -> water, 2 -> grass
  22.            
  23.             for (int x = l; x <= m; x++){
  24.                 if (dp[l][m][x]){
  25.                     if (s[x] == 'F') wx[0]++;
  26.                     else if (s[x] == 'W') wx[1]++;
  27.                     else wx[2]++;
  28.                 }
  29.             }
  30.            
  31.             for (int y = m + 1; y <= r; y++){
  32.                 if (dp[m + 1][r][y]){
  33.                     if (s[y] == 'F') wy[0]++;
  34.                     else if (s[y] == 'W') wy[1]++;
  35.                     else wy[2]++;
  36.                 }
  37.             }
  38.            
  39.             for (int x = l; x <= m; x++){
  40.                 if (dp[l][m][x]){
  41.                     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
  42.                     if (s[x] == 'W' && (wy[1] > 0 || wy[0] > 0)) dp[l][r][x] = true;
  43.                     if (s[x] == 'G' && (wy[2] > 0 || wy[1] > 0)) dp[l][r][x] = true;
  44.                 }
  45.             }
  46.            
  47.             for (int y = m + 1; y <= r; y++){
  48.                 if (dp[m + 1][r][y]){
  49.                     if (s[y] == 'F' && (wx[0] > 0 || wx[2] > 0)) dp[l][r][y] = true;
  50.                     if (s[y] == 'W' && (wx[1] > 0 || wx[0] > 0)) dp[l][r][y] = true;
  51.                     if (s[y] == 'G' && (wx[2] > 0 || wx[1] > 0)) dp[l][r][y] = true;
  52.                 }
  53.             }
  54.         }
  55.     }
  56.    
  57.     int ans = 0;
  58.     for (int i = 0; i < n; i++) ans += dp[0][n - 1][i]; // add 1 if i is possible winner in whole array
  59.    
  60.     cout << ans << "\n";
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement