poj 2250Compromise
2012-08-06 09:56
113 查看
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; char text1[110][32]; int num1; char text2[110][32]; int num2; char ans[110][32]; int len; #define left 1 #define up 2 #define upleft 3 int d[110][110]; int b[110][110]; void dp(int l1,int l2) { if(strcmp(text1[l1-1],text2[l2-1])==0) { if(d[l1-1][l2-1]==-1) { dp(l1-1,l2-1); } d[l1][l2]=d[l1-1][l2-1]+1; b[l1][l2]=upleft; return; } if(d[l1-1][l2]==-1) { dp(l1-1,l2); } if(d[l1][l2]<d[l1-1][l2]) { d[l1][l2]=d[l1-1][l2]; b[l1][l2]=up; } if(d[l1][l2-1]==-1) { dp(l1,l2-1); } if(d[l1][l2]<d[l1][l2-1]) { d[l1][l2]=d[l1][l2-1]; b[l1][l2]=left; } } void back() { int l1=num1; int l2=num2; while(1) { if(l1==0||l2==0) { break; } if(b[l1][l2]==upleft) { strcpy(ans[len++],text1[l1-1]); l1--; l2--; continue; } if(b[l1][l2]==up) { l1--; continue; } if(b[l1][l2]=left) { l2--; continue; } } } int main() { char temp[32]; while(scanf("%s",temp)!=EOF) { strcpy(text1[0],temp); num1=1; getchar(); while(1) { scanf("%s",text1[num1++]); getchar(); if(strcmp(text1[num1-1],"#")==0) { num1--; break; } } num2=0; while(1) { scanf("%s",text2[num2++]); getchar(); if(strcmp(text2[num2-1],"#")==0) { num2--; break; } } memset(d,-1,sizeof(d)); int i; for(i=0;i<=num1;i++) { d[i][0]=0; } for(i=0;i<=num2;i++) { d[0][i]=0; } dp(num1,num2); len=0; back(); for(i=len-1;i>0;i--) { printf("%s ",ans[i]); } printf("%s\n",ans[0]); } return 0; }
算法导论有详解
相关文章推荐
- poj-2250Compromise(LCS+标记数组)
- poj_2250Compromise
- poj_2250Compromise
- POJ 2250Compromise(DP最长子公共子序列)
- poj 3368 Frequent values
- POJ 3696 The Luckiest number
- poj 3006 - Dirichlet's Theorem on Arithmetic Progressions
- POJ 3630 静态字典树
- POJ 3080 Blue Jeans(KMP)
- POJ 1088 滑雪(DP+记忆化搜索)
- poj 1975 Median Weight Bead
- POJ 2019 Cornfields
- poj 3620 深搜
- POJ-1088 滑雪 记忆化搜索
- POJ 3311 Hie with the Pie (状压DP)
- poj 1469——COURSES(匹配)
- poj 3648 Wedding
- (简单) POJ 1847 Tram,Dijkstra。
- POJ 2612
- POJ-3984-迷宫问题