Morass

Common Subsequence

Jun 17th, 2019
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define PB push_back
  4. #define ZERO (1e-10)
  5. #define INF int(1e9+1)
  6. #define CL(A,I) (memset(A,I,sizeof(A)))
  7. #define DEB printf("DEB!\n");
  8. #define D(X) cout<<"  "<<#X": "<<X<<endl;
  9. #define EQ(A,B) (A+ZERO>B&&A-ZERO<B)
  10. typedef long long ll;
  11. typedef pair<ll,ll> pll;
  12. typedef vector<int> vi;
  13. typedef pair<int,int> ii;
  14. typedef vector<ii> vii;
  15. #define IN(n) int n;scanf("%d",&n);
  16. #define FOR(i, m, n) for (int i(m); i < n; i++)
  17. #define F(n) FOR(i,0,n)
  18. #define FF(n) FOR(j,0,n)
  19. #define FT(m, n) FOR(k, m, n)
  20. #define aa first
  21. #define bb second
  22. void ga(int N,int *A){F(N)scanf("%d",A+i);}
  23. #define MOD 1000000007
  24. #define MX 2048
  25. int dp[MX][MX][2][2],N,M,A[MX],B[MX];
  26. int dyn(int a,int b,bool x,bool y){
  27.     if(a==N||b==M)return !x&&!y;
  28.     int&v=dp[a][b][x][y];
  29.     if(~v)return v;
  30.     v=0;
  31.     if(A[a]==B[b])v=dyn(a+1,b+1,0,0);
  32.     if(!x&&!y)v=(v+dyn(a+1,b+1,0,0))%MOD;
  33.     if(x)v=(v+dyn(a,b+1,1,0))%MOD;
  34.     if(y)v=(v+dyn(a+1,b,0,1))%MOD;
  35.     if(!x&&!y)v=(v+ll(dyn(a+1,b,0,1))+dyn(a,b+1,1,0))%MOD;
  36.     return v;
  37. }
  38. int main(void){
  39.     scanf("%d%d",&N,&M),ga(N,A),ga(M,B),CL(dp,-1);
  40.     printf("%d\n",dyn(0,0,0,0));
  41.     return 0;
  42. }
Add Comment
Please, Sign In to add comment