UVa 111 - History Garding

来源:互联网 发布:软件开发技术 编辑:程序博客网 时间:2024/06/08 13:36

题目链接 :UVa 111 History Garding

动态规划,最长公共子序列。很久之前做的一道题,今天整理东西把这篇也发上来。动态规划的题目掌握的不好,得加把劲。

/**LCS*/#include<iostream>#include<cstring>using namespace std;int x[30],y[30];int dp[30][30];int main(){    int n,temp;    cin>>n;    for(int i=1;i<=n;i++)    {        cin>>temp;        x[temp]=i;    }    while(cin>>temp)    {        y[temp]=1;        for(int i=2;i<=n;i++)        {            cin>>temp;            y[temp]=i;        }        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++)            for(int j=1;j<=n;j++)            {                if(x[i]==y[j])                    dp[i][j]=dp[i-1][j-1]+1;                else                    dp[i][j]=max(dp[i-1][j],dp[i][j-1]);            }        cout<<dp[n][n]<<endl;    }    return 0;}


0 0
原创粉丝点击