poj 2250 Compromise 记录路径的lcs

记录路径的lcs

ACcode:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#define maxn 101
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
struct N{
    char s[33];
};
N a[maxn],b[maxn];
char tmp[33];
int lena,lenb,lenans;
int dp[maxn][maxn];
struct NN{
    int num[maxn];
    int tot;
}ans[maxn][maxn];
int main(){
    while(scanf("%s",tmp)!=EOF){
        lena=lenb=1;
        strcpy(a[lena++].s,tmp);
        memset(dp,0,sizeof(dp));
        memset(ans,0,sizeof(ans));
        while(scanf("%s",a[lena].s)&&a[lena++].s[0]!='#');
        while(scanf("%s",b[lenb].s)&&b[lenb++].s[0]!='#');
        for(int i=1;i<lena;++i)
            for(int j=1;j<lenb;++j){
                if(strcmp(a[i].s,b[j].s)==0){
                    dp[i][j]=dp[i-1][j-1]+1;
                    for(int k=0;k<ans[i-1][j-1].tot;++k)
                        ans[i][j].num[k]=ans[i-1][j-1].num[k];
                    ans[i][j].tot=ans[i-1][j-1].tot;
                    ans[i][j].num[ans[i][j].tot++]=i;
                }
                else {
                    if(dp[i-1][j]>dp[i][j-1]){
                        dp[i][j]=dp[i-1][j];
                        for(int k=0;k<ans[i-1][j].tot;++k)
                            ans[i][j].num[k]=ans[i-1][j].num[k];
                            ans[i][j].tot=ans[i-1][j].tot;
                    }
                    else {
                        dp[i][j]=dp[i][j-1];
                        for(int k=0;k<ans[i][j-1].tot;++k)
                            ans[i][j].num[k]=ans[i][j-1].num[k];
                            ans[i][j].tot=ans[i][j-1].tot;
                    }
                }
        }
        //cout<<dp[lena-1][lenb-1]<<'\12';
        for(int i=0;i<ans[lena-1][lenb-1].tot-1;++i,putchar(' '))
            printf("%s",a[ans[lena-1][lenb-1].num[i]].s);
        putchar('\n');
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值