light oj 1110 LCS 记录路径

来源:互联网 发布:手机 惯性导航算法 编辑:程序博客网 时间:2022/08/16 18:57
#include <stdio.h>#include <string.h>#include <algorithm>#pragma warning (disable : 4996)using namespace std;const int Max = 105;int dp[Max][Max];char s[Max][Max][Max];char str1[Max], str2[Max];int main(){int T, k, i, j;scanf("%d", &T);for (int t = 1; t <= T; t++){memset(s, 0, sizeof(s));scanf("%s %s", str1+1, str2+1);for (i = 1; str1[i]; i++){for (j = 1; str2[j]; j++){if (str1[i] == str2[j]){dp[i][j] = dp[i - 1][j - 1] + 1;for (k = 0; k < dp[i - 1][j - 1]; k++)s[i][j][k] = s[i - 1][j - 1][k];s[i][j][k] = str1[i];}else{if (dp[i - 1][j]>dp[i][j - 1]){dp[i][j] = dp[i - 1][j];strcpy(s[i][j], s[i - 1][j]);}else if (dp[i][j - 1] > dp[i - 1][j]){dp[i][j] = dp[i][j - 1];strcpy(s[i][j], s[i][j - 1]);}else{dp[i][j] = dp[i - 1][j];if (strcmp(s[i - 1][j], s[i][j - 1]) >= 0)strcpy(s[i][j], s[i][j - 1]);elsestrcpy(s[i][j], s[i - 1][j]);}}}}i--; j--;printf("Case %d: ", t);if (dp[i][j])puts(s[i][j]);else puts(":(");}return 0;}

0 0
原创粉丝点击