最长回文子序列

    技术2024-03-20  81

    题目:Two Rabbits HDU - 4745

    分析:

    1、先把它当成直线而不是圆,求出各个区间的最长回文子序列 2、考虑圆环:将其分成两段 如图:

    代码:

    #include <bits/stdc++.h> using namespace std; typedef long long ll; //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)) const ll mod = 1e9 + 7; const int maxn = 2e6; const int inf = 0x3f3f3f3f; int n; int a[2010]; int dp[2010][2010]; int main() { while(cin >> n && n) { for(int i = 1; i <= n; i++) cin >> a[i]; for(int i = 1; i <= n; i++) dp[i][i] = 1; for(int len = 2; len <= n; len++) { for(int i = 1, j = i + len - 1; j <= n; i++, j++) { dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); if(a[i] == a[j]) dp[i][j] = max(dp[i][j], 2 + dp[i + 1][j - 1]); } } int res = 0; for(int i = 1; i <= n; i++) res = max(res, dp[1][i] + dp[i + 1][n]); cout << res << endl; } }
    Processed: 0.014, SQL: 9