标签:
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 2158 | Accepted: 1066 | Special Judge |
Description
Input
Output
Sample Input
apple peach ananas banana pear peach
Sample Output
appleach bananas pearch
Source
#include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm> using namespace std; int dp[105][105]; int flag[105][105]; char str[105],str1[105]; char P[105]; int k; void print(int l,int r) { if(l == 0 && r == 0) return; if(flag[l][r] == 0) { print(l-1,r-1); printf("%c",str[l]); } else if(flag[l][r] == 1) { print(l-1,r); printf("%c",str[l]); } else { print(l,r-1); printf("%c",str1[r]); } } void LCS() { memset(flag,0,sizeof(flag)); int n= strlen(str+1); int m = strlen(str1+1); for(int i = 1; i <= n; i++) ///初始化不能少 flag[i][0] = 1; for(int i = 1; i <= m; i++) ///same flag[0][i] = -1; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(str[i]==str1[j]) { dp[i][j]=dp[i-1][j-1]+1; flag[i][j]=0; } else { if(dp[i-1][j]>dp[i][j-1]) { dp[i][j]=dp[i-1][j]; flag[i][j]=1; } else { dp[i][j]=dp[i][j-1]; flag[i][j]=-1; } } } } print(n,m); printf("\n"); } int main() { while(scanf("%s%s",str+1,str1+1)!=EOF) { LCS(); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5425272.html