Description
Once upon a time, there was an ancient empire with two towers of different shapes in different cities. The two towers are built with a circular brick. The height of these circular bricks is the same, and the radius is an integer. Obviously, although the two towers have different shapes, many of the circular bricks used by the towers are the same.
After many years, a king ordered his architects to remove some bricks from two towers so that they could be in the same shape and make the two towers as high as possible, in addition, the order of the bricks and tiles of the new tower must be the same as that of the original one. The king thought that these two towers were symbolic of harmony and equality between the two cities and named them Twin Towers.
Task
Now, two thousand years later, you can solve this very simple problem: To give you a description of two towers of different shapes, you only need to calculate the maximum number of bricks and tiles that can be built into the new Twin Towers.
-
Input
-
The input includes the following: enter two integers N1 and N2 (1 <= N1, N2 <= 100) in the first line to indicate the number of tiles of the first two towers; in the second line, enter a positive integer N1 to indicate the radius of the first tile (arranged in the order from top to bottom ); enter N2 integers in the following line to indicate the radius of the second tower tile (arranged in the order from top to bottom ).
It ends when N1 and N2 are 0.
-
Output
-
Output the maximum number of bricks and tiles for the new Twin Towers.
Simulation questions
#include<iostream>using namespace std;int a[100];int b[100];int f(int ,int ,int ,int);int main(){ int A,B; cin>>A>>B; int s; int n=0; while(A||B) { n++; for(int i=0;i<A;i++) cin>>a[i]; for(int i=0;i<B;i++) cin>>b[i]; s=f(0,A,0,B); cout<<"Twin Towers #"<<n<<endl; cout<<"Number of Tiles : "<<s<<endl; cin>>A>>B; } return 0;}int f(int i1,int j1,int i2,int j2){ int s; if(i1<j1&&i2<j2) { if(a[i1]==b[i2]) { s=1+f(i1+1,j1,i2+1,j2); } else s=max(f(i1+1,j1,i2,j2),f(i1,j1,i2+1,j2)); return s; } else return 0;}