Advertisement
SuitNdtie

TOI11: Segitiga

May 29th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.69 KB | None | 0 0
  1. #include<stdio.h>
  2. int const N = 257;
  3. char str[257];
  4. bool dp[257][257][3];
  5. bool vst[257][257][3];
  6.        
  7. bool cal(int L,int R,int val){
  8. //  printf("Test (%d,%d)%d\n",L,R,val);
  9.     if(L == R){
  10.         return str[L]-'0' == val;
  11.     }
  12.     if(R - L == 1){
  13.         if(val == 0){
  14.             return (str[L] == '0' && str[R] == '2');
  15.         }
  16.         else if(val == 1){
  17.             return  (str[L] == '0' && str[R] == '1')||
  18.                     (str[L] == '1' && str[R] == '1')||
  19.                     (str[L] == '1' && str[R] == '2')||
  20.                     (str[L] == '2' && str[R] == '0')||
  21.                     (str[L] == '2' && str[R] == '2');
  22.         }
  23.         else if(val == 2){
  24.             return  (str[L] == '0' && str[R] == '0')||
  25.                     (str[L] == '1' && str[R] == '0')||
  26.                     (str[L] == '2' && str[R] == '1');
  27.         }
  28.         else{
  29.             printf("error (%d,%d) %d\n",L,R,val);
  30.             return false;
  31.         }
  32.     }
  33.     if(vst[L][R][val])return dp[L][R][val];
  34.     vst[L][R][val] = true;
  35.     bool ans = false;
  36.     for(int m = L ; m < R && !ans ; m ++){
  37.         if(val == 0){
  38.             ans |= (cal(L,m,0) && cal(m+1,R,2));
  39.         }
  40.         else if(val == 1){
  41.             ans |=  (cal(L,m,0) && cal(m+1,R,1))||
  42.                     (cal(L,m,1) && cal(m+1,R,1))||
  43.                     (cal(L,m,1) && cal(m+1,R,2))||
  44.                     (cal(L,m,2) && cal(m+1,R,0))||
  45.                     (cal(L,m,2) && cal(m+1,R,2));
  46.         }
  47.         else if(val == 2){
  48.             ans |=  (cal(L,m,0) && cal(m+1,R,0))||
  49.                     (cal(L,m,1) && cal(m+1,R,0))||
  50.                     (cal(L,m,2) && cal(m+1,R,1));
  51.         }
  52.     }
  53.     //printf("Test [%d,%d] need %d - > %s\n",L,R,val,(ans ? "true" : "false"));
  54.     return dp[L][R][val] = ans;
  55. }
  56.  
  57. int main()
  58. {
  59.     for(int t = 0 ; t < 20 ; t ++){
  60.         int n;
  61.         for(int i = 0 ; i < 257 ; i ++)for(int j = 0 ; j < 257 ; j ++)for(int k = 0 ; k < 3 ; k ++)vst[i][j][k] = false;
  62.         scanf("%d",&n);
  63.         scanf(" %s",str);
  64.         printf("%s\n",cal(0,n-1,0) ? "yes" : "no");
  65.     }
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement