poj 2250 Compromise dp求lcs+输出方案

来源:互联网 发布:mac管理员名称和密码 编辑:程序博客网 时间:2024/05/01 22:22
//poj 2250//sep9#include <iostream>using namespace std;struct WORD{char s[32];}a[128],b[128];int dp[128][128],match[128][128];void print(int x,int y){if(x==0||y==0)return ;if(match[x][y]==0){print(x-1,y-1);printf("%s ",a[x].s);}else if(match[x][y]==1){print(x,y-1);}else if(match[x][y]==2){print(x-1,y);}}int main(){while(1){int t=1,p=1,q=1;while(1){t=scanf("%s",a[p++].s);if(t==-1) break;if(a[p-1].s[0]=='#'){--p;break;}}if(t==-1) break;while(1){scanf("%s",b[q++].s);if(b[q-1].s[0]=='#'){--q;break;}}memset(dp,0,sizeof(dp));for(int i=1;i<p;++i)for(int j=1;j<q;++j){if(!strcmp(a[i].s,b[j].s))dp[i][j]=dp[i-1][j-1]+1,match[i][j]=0;else{dp[i][j]=dp[i][j-1];match[i][j]=1;if(dp[i][j]<dp[i-1][j])dp[i][j]=dp[i-1][j],match[i][j]=2;}}print(p-1,q-1);printf("\n");}return 0;} 

0 0
原创粉丝点击