Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include<cstdio>
- using namespace std;
- int a[105],b[105],dp[105][105];
- int n,m;
- int LCS(int i, int j)
- {
- if(i==n||j==m) return 0;
- if(dp[i][j]!=-1) return dp[i][j];
- if(a[i]==b[j]) return dp[i][j]=1+LCS(i+1,j+1);
- int x,y;
- x=LCS(i+1,j);
- y=LCS(i,j+1);
- return dp[i][j]=max(x,y);
- }
- int main()
- {
- int kase=0;
- while(1)
- {
- memset(dp,-1,sizeof dp);
- scanf("%d%d",&n,&m);
- if(n==0&&m==0) break;
- for(int i=0; i<n; i++)
- scanf("%d",&a[i]);
- for(int i=0; i<m; i++)
- scanf("%d",&b[i]);
- int ans=LCS(0,0);
- printf("Twin Towers #%d\nNumber of Tiles : %d\n",++kase,ans);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement