UVA 10066 The Twin Towers

2020-12-13 15:57

阅读:370

标签:lcs   dp   动态规划   

DP,题目很长,题意就是求LCS。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i=b;i--)
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define acfun std::ios::sync_with_stdio(false)

#define SIZE 100+1
using namespace std;

int a[SIZE],b[SIZE],dp[SIZE][SIZE];

int main()
{
    int la,lb;
    int ca=1;
    while(~sf("%d%d",&la,&lb),la||lb)
    {
        FOR(i,0,la)
        sf("%d",&a[i]);
        FOR(i,0,lb)
        sf("%d",&b[i]);

        CLR(dp,0);
        FOR(i,0,la)
        FOR(j,0,lb)
        {
            if(a[i]==b[j])
                dp[i+1][j+1]=dp[i][j]+1;
            else
                dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
        }
        pf("Twin Towers #%d\n",ca++);
        pf("Number of Tiles : %d\n\n",dp[la][lb]);
    }
}

UVA 10066 The Twin Towers

标签:lcs   dp   动态规划   

原文地址:http://blog.csdn.net/dongshimou/article/details/40855427


评论


亲,登录后才可以留言!