Light oj 1110 LCS带打印路径

来源:互联网 发布:java sha1解密 编辑:程序博客网 时间:2024/04/26 07:23

这个LCS打印路径用string类最方便感觉!!!

#include<cstring>#include<cstdio>#include<iostream>#include<cmath>#include<algorithm>#include<queue>#include<vector>using namespace std;#define mod 1000000007#define PI acos(-1.0)#define INF 0x3f3f3f3ftypedef long long LL;int n,lena,lenb;int dp[105][105];int path[105][105];char a[105],b[105];void print_path(int x,int y){    if(x==0||y==0)return ;    else if(path[x][y]==0){        print_path(x-1,y-1);        printf("%c",a[x-1]);    }    else if(path[x][y]==1){        print_path(x-1,y);    }    else {        print_path(x,y-1);    }}int main(){    scanf("%d",&n);    for(int k=1;k<=n;k++){        scanf("%s%s",a,b);        lena=strlen(a);        lenb=strlen(b);        memset(dp,0,sizeof(dp));        memset(path,0,sizeof(path));        for(int i=1;i<=lena;i++){            for(int j=1;j<=lenb;j++){                if(a[i-1]==b[j-1]){                    dp[i][j]=dp[i-1][j-1]+1;                    path[i][j]=0;                }                else if(dp[i-1][j]>dp[i][j-1]){                    path[i][j]=1;                    dp[i][j]=dp[i-1][j];                }                else {                    path[i][j]=-1;                    dp[i][j]=dp[i][j-1];                }            }        }        printf("Case %d: ",k);        if(dp[lena][lenb]==0)printf(":(");        else print_path(lena,lenb);        printf("\n\n");    }    return 0;}
0 0
原创粉丝点击