dp[i][j][k][p]表示a串的[i, j]和b串的[k, p]能否通過交叉且保持原有順序不變形成回文串。
4維dp, 先列舉長度,再列舉位置,狀態合法則貢獻答案。
#include
using
namespace std;
typedef
long
long ll;
//三年競賽一場空,不開long long見祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define x first
#define y second
typedef pair<
int,
int> par;
const ll mod =
1e9+7;
const
int maxn =55;
const
int inf =
0x3f3f3f3f
;char a[maxn]
, b[maxn]
;int dp[maxn]
[maxn]
[maxn]
[maxn]
;int
main()
if(len1 >=
2&& a[i]
== a[j]
) v |
= dp[i +1]
[j -1]
[k][p];if
(len2 >=
2&& b[k]
== b[p]
) v |
= dp[i]
[j][k +1]
[p -1]
;if(len1 && len2 && a[j]
== b[k]
) v |
= dp[i]
[j -1]
[k +1]
[p];
if(len1 && len2 && a[i]
== b[p]
) v |
= dp[i +1]
[j][k]
[p -1]
;if(v) res =
max(res, len1 + len2)
;}
cout << res << endl;
}}
合併回文子串 區間dp
題目描述 輸入兩個字串a和b,合併成乙個串c,屬於a和b的字元在c中順序保持不變。如 abc 和 xyz 可以被組合成 axbycz 或 abxcyz 等。我們定義字串的價值為其最長回文子串的長度 回文串表示從正反兩邊看完全一致的字串,如 aba 和 xyyx 需要求出所有可能的c中價值最大的字串,...
NC13230 合併回文子串(區間dp)
從資料範圍不難推出可以用f,表示由兩個字串來表示的最長大小 並且因為是回文串,所以我們要向頭尾加字元,來變大,因為這個是回文子串,也就是連續的一段。include include include include include using namespace std const int n 55 c...
合併回文子串
學習周記 時間限制 c c 2秒,其他語言4秒 空間限制 c c 262144k,其他語言524288k 64bit io format lld 題目描述 輸入兩個字串a和b,合併成乙個串c,屬於a和b的字元在c中順序保持不變。如 abc 和 xyz 可以被組合成 axbycz 或 abxcyz 等...