Advertisement
RaFiN_

Erase Subsequences cf-1303E

Jul 5th, 2020
1,193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1.  
  2. //Erase Subsequences cf-1303E
  3. const int N = 404;
  4. const short inf = 2000;
  5.  
  6. char s[N], t[N];
  7. short dp[N][N][N], vis[N][N][N],cn,ls,lt;
  8.  
  9. short rec(int p,int i,int j)
  10. {
  11.     if (p==ls){
  12.         if (i==lt || j==lt) return 0;
  13.         return -inf;
  14.     }
  15.     short &ret = dp[p][i][j];
  16.     short &vs = vis[p][i][j];
  17.     if (vs==cn) return ret;
  18.     vs = cn;
  19.     ret = -inf;
  20.     int x = 0;
  21.     if (i<lt && s[p]==t[i]) x = 1,ret = max(ret,(short) (rec(p+1,i+1,j)+1));
  22.     if (j<lt && s[p]==t[j]) x = 1,ret = max(ret, (short)(rec(p+1,i,j+1)+1));
  23.     if (!x) ret = rec(p+1,i,j);
  24.     return ret;
  25. }
  26.  
  27. int main()
  28. {
  29.     int tc; scanf("%d",&tc);
  30.     while (tc--)
  31.     {
  32.         scanf("%s%s",s,t);
  33.         ls = strlen(s);
  34.         lt = strlen(t);
  35.         int mx = 0;
  36.         cn++;
  37.         for (int i = 0;i<lt;i++)
  38.             mx = max(mx, (int)rec(0,0,i));
  39.         if (mx>=lt) printf("YES\n");
  40.         else printf("NO\n");
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement