poj 2127(最长公共上升子序列)
2014-02-18 09:53
211 查看
题目链接:点击打开链接
题目分析:1.设主串为father【i】,子串为son【j】,dp【I】【J】表示主串以father【i】结尾和son【j】结尾的区间有当前的最大上升子列。
尽量压缩主串比如:若father【i】==son【j】,则考虑在i前的k位和j-1是不是有更优的选择,找到最大值然后+1就是dp【i】【j】
若不相等。则dp[i][i]=dp【i】【j-1】
我觉得这题最关键的处理是关于路径的保存,用了二维和递归的方法
题目分析:1.设主串为father【i】,子串为son【j】,dp【I】【J】表示主串以father【i】结尾和son【j】结尾的区间有当前的最大上升子列。
尽量压缩主串比如:若father【i】==son【j】,则考虑在i前的k位和j-1是不是有更优的选择,找到最大值然后+1就是dp【i】【j】
若不相等。则dp[i][i]=dp【i】【j-1】
我觉得这题最关键的处理是关于路径的保存,用了二维和递归的方法
#include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> using namespace std; struct Node { int x,y; }path[505][505]; int father[505],son[505]; int dp[505][505]; int ans_x,ans_y; void print (int m,int n) { if(m==-1||n==-1) return; int x= path[m] .x; int y= path[m] .y; if(dp[x][y]>0) print(x,y); if(father[m]==son ) printf("%d ",father[m]); } int solve(int m,int n) { int i,j, max=0; for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { int x=-1,y=-1,tmp=0; if(father[i]==son[j]) { for(int k=i-1;k>=1;k--) { if(father[k]<father[i]&&dp[k][j-1]>tmp) { tmp=dp[k][j-1]; x=k,y=j-1; } } tmp++; } else { tmp=dp[i][j-1]; x=i,y=j-1; } dp[i][j]=tmp; path[i][j].x=x; path[i][j].y=y; if(max<tmp) { max=tmp; ans_x=i,ans_y=j; } } } return max; } int main() { int m,n,res,i; while(scanf("%d",&m)!=EOF) { for( i=1;i<=m;i++) { scanf("%d",&father[i]); } scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&son[i]); } memset(dp,0,sizeof(dp)); memset(path,-1,sizeof(path)); res= solve(m, n); printf("%d\n",res); if(res!=0) print(ans_x,ans_y); printf("\n"); } return 0; }
相关文章推荐
- POJ 2127 最长公共上升子序列
- POJ 2127 Greatest Common Increasing Subsequence (最长公共上升子序列+记录路径)
- POJ 2127 最长公共上升子序列
- poj 2127 Greatest Common Increasing Subsequence(最长公共上升子序列dp)
- 【poj 2127】Greatest Common Increasing Subsequence 最长公共上升子序列lics+路径打印
- poj 2127 dp(最长公共递增子序列)
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
- 最长公共上升子序列(LCIS)学习小记 Hdu 1423 + Poj 2127 (LCIS路径输出)
- poj3903 Stock Exchange(最长上升子序列)
- poj 1631 Bridging signals 最长上升子序列的O(n*lgn)方法
- POJ - 2533 Longest Ordered Subsequence —— 最长不连续上升子序列
- 简单的dp@POJ(3)2533 最长上升子序列
- poj 2533 最长上升子序列
- POJ2533, 最长上升子序列(贪心+二分查找时间复杂度O(nlogn))
- poj 1065 最长上升子序列
- 「HZOI 2016」最长公共上升子序列
- poj 2533 Longest Ordered Subsequence(最长上升子序列)
- 最长公共上升子序列
- POJ 1631 最长上升子序列O(nlogn)
- [dp](不连续)最大公共上升子序列 POJ 2127