//Problem link >>http://uva.onlinejudge.org/external/100/10066.html
.. simple DP problem ... implementing straight LCS ....
#include <iostream>
#include <vector>
using namespace std ;
int main ()
{
int x , y ,a , b , cas=0 ;
int lcs[103][103] ;
vector<int>v1 ;
vector<int>v2 ;
while (cin>>x>>y && x||y)
{
v1.clear() ;
v2.clear() ;
for (int i=0 ; i<x ; i++)
{
cin>>a ;
v1.push_back(a) ;
}
for (int i=0 ; i<y ; i++)
{
cin>>a ;
v2.push_back(a) ;
}
for (int i=0 ; i<x ; i++) lcs[i][0]=0 ;
for (int i=0 ; i<y ; i++) lcs[0][i]=0 ;
for (int i=1 ; i<=x ; i++)
{
for (int j=1 ; j<=y ; j++)
{
if (v1[i-1]==v2[j-1]) lcs[i][j]=1+lcs[i-1][j-1] ;
else lcs[i][j]=max(lcs[i][j-1] , lcs[i-1][j]) ;
}
}
cout<<"Twin Towers #"<<++cas<<endl ;
cout<<"Number of Tiles : "<<lcs[x][y]<<endl<<endl ;
}
return 0 ;
}
20 15 10 15 25 20 15
15 25 10 20 15 20
8 9
10 20 20 10 20 10 20 10
20 10 20 10 10 20 10 10 20
Number of Tiles : 4
Twin Towers #2
Number of Tiles : 6
.. simple DP problem ... implementing straight LCS ....
#include <iostream>
#include <vector>
using namespace std ;
int main ()
{
int x , y ,a , b , cas=0 ;
int lcs[103][103] ;
vector<int>v1 ;
vector<int>v2 ;
while (cin>>x>>y && x||y)
{
v1.clear() ;
v2.clear() ;
for (int i=0 ; i<x ; i++)
{
cin>>a ;
v1.push_back(a) ;
}
for (int i=0 ; i<y ; i++)
{
cin>>a ;
v2.push_back(a) ;
}
for (int i=0 ; i<x ; i++) lcs[i][0]=0 ;
for (int i=0 ; i<y ; i++) lcs[0][i]=0 ;
for (int i=1 ; i<=x ; i++)
{
for (int j=1 ; j<=y ; j++)
{
if (v1[i-1]==v2[j-1]) lcs[i][j]=1+lcs[i-1][j-1] ;
else lcs[i][j]=max(lcs[i][j-1] , lcs[i-1][j]) ;
}
}
cout<<"Twin Towers #"<<++cas<<endl ;
cout<<"Number of Tiles : "<<lcs[x][y]<<endl<<endl ;
}
return 0 ;
}
Sample Input
7 620 15 10 15 25 20 15
15 25 10 20 15 20
8 9
10 20 20 10 20 10 20 10
20 10 20 10 10 20 10 10 20
Sample Output
Twin Towers #1Number of Tiles : 4
Twin Towers #2
Number of Tiles : 6
No comments:
Post a Comment