51nod 最長公共子串行測試 LCS 回溯

2021-07-22 10:53:50 字數 1107 閱讀 5105

輸入

第1行:字串a

第2行:字串b

(a,b的長度 <= 1000)

輸出

輸出最長的子串行,如果有多個,隨意輸出1個。

輸入示例

abcicba

abdkscab

輸出示例

abca

這道題比較6,但是利用每個位置的記錄進行回溯就更6,好好體會下。

#include #include #include #include #include #include #include #include #include #include #include #include //#include //#define loacl

#define space " "

using namespace std;

//typedef long long long;

//typedef __int64 int;

typedef pairpaii;

const int inf = 0x3f3f3f3f;

const double esp = 1e-6;

const double pi = acos(-1.0);

const int mod = 1e9 + 7;

const int maxn = 1000 + 5;

char str1[maxn], str2[maxn];

int dp[maxn][maxn], vis[maxn][maxn];

void print(int x, int y)

else if (vis[x][y] == 2)

else

}int main()

else if (dp[i - 1][j] > dp[i][j - 1])

else }}

print(len1, len2); printf("\n");

}return 0;

}

51NOD 最長公共子串行問題

給出兩個字串a b,求a與b的最長公共子串行 子串行不要求是連續的 比如兩個串為 abcicba abdkscab ab是兩個串的子串行,abc也是,abca也是,其中abca是這兩個字串最長的子串行。輸入 第1行 字串a 第2行 字串b a,b的長度 1000 輸出 輸出最長的子串行,如果有多個,...

51NOD 最長公共子串行問題

最長公共子串行問題就是求序列a a1,a2,an,和b b1,b2,bm,的乙個最長公共子串行。因為最長公共子串行不唯一,讓我們把問題簡化,如何求出兩個序列的最長公共子串行長度呢?你首先能想到的恐怕是暴力列舉?那我們先來看看 序列a有 2 n 個子序列,序列b有 2 m 個子序列,如果任意兩個子串行...

51nod動態規劃入門 最長公共子串行

題目 給出兩個字串a b,求a與b的最長公共子串行 子串行不要求是連續的 輸入 第1行 字串a 第2行 字串b a,b的長度 1000 輸出 輸出最長的子串行,如果有多個,隨意輸出1個。輸入示例 abcicba abdkscab 輸出示例 abca 如下 include include includ...