File size: 914 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
  >>~~ UVa Online Judge ACM Problem Solution ~~<<

  ID: 10066
  Name: The Twin Towers
  Problem: https://onlinejudge.org/external/100/10066.pdf
  Language: C++

  Author: Arash Shakery
  Email: arash.shakery@gmail.com
*/

#include <stdio.h>
#include <iostream>
using namespace std;

int a[102], b[102], DP[102][102];
int main(){
    int n, m, cse=0;

    while (cin>>n>>m && (n||m)) {
        for (int i=1; i<=n; i++)
            cin >> a[i];

        for (int j=1; j<=m; j++) {
            cin >> b[j];
            for (int i=1; i<=n; i++)
                DP[i][j]=0;
        }

        for (int i=1; i<=n; i++)
            for (int j=1; j<=m; j++)
                if (a[i] == b[j])
                    DP[i][j] = DP[i-1][j-1] + 1;
                else if (j>0)
                    DP[i][j] = max(DP[i-1][j], DP[i][j-1]);

        printf("Twin Towers #%d\nNumber of Tiles : %d\n\n", ++cse, DP[n][m]);
    }
}