栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 2432 Greatest Common Incr...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 2432 Greatest Common Incr...

#include <cstdio>#include <cstring>using namespace std;#define N 550struct node{ int x,y;}path[N][N];int dp[N][N],s[N],t[N];int n,m;int main(){ int T; scanf("%d",&T); while(T--){ memset(path,0,sizeof(path)); scanf("%d",&n); for(int i = 1; i <= n; i++) scanf("%d",&s[i]); scanf("%d",&m); for(int i = 1; i <= m; i++) scanf("%d",&t[i]); memset(dp,0,sizeof(dp)); int mx = 0; for(int i = 1; i <= n; i++){ mx = 0; int tx = 0,ty = 0; for(int j = 1; j <= m; j++){ dp[i][j] = dp[i-1][j]; path[i][j].x = i-1; path[i][j].y = j; if(s[i] > t[j] && mx < dp[i-1][j]){ mx = dp[i-1][j]; tx = i-1; ty = j; } if(s[i]==t[j]){ dp[i][j] = mx+1; path[i][j].x = tx; path[i][j].y = ty; } } } mx = -1; int id; for(int i = 1; i <= m; i++) if(dp[n][i] > mx){ mx = dp[n][i]; id = i; } int save[N]; int cnt = 0; int tx = n,ty = id; while(dp[tx][ty] != 0) { int tmpx,tmpy; tmpx = path[tx][ty].x; tmpy = path[tx][ty].y; if(dp[tx][ty] != dp[tmpx][tmpy]){ save[cnt++] = t[ty]; } tx = tmpx; ty = tmpy; } printf("%dn",mx); for(int i =cnt-1; i >= 0; i--) printf("%d ",save[i]); printf("n"); } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/379090.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号