Question: two rabbits jumped on n blocks of a ring Stone. Each stone had a weight of ai. One jumped from left to right, and the other jumped from right to left, for each hop, the weights of the stones where the two rabbits are located must be equal and within the same circle (each of them cannot exceed its own starting point or return to its starting point again) the maximum number of stones they can pass (1 <= n <= 1000, 1 <= ai <= 1000 ). Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 4745 --> after the simulation sample, the first look is like the Euler loop, then the students say it should be the longest public subsequence LCS, and then it's miserable until the game's Ended all TLE ...... Originally, it was just a simple dp to find the longest echo sub-sequence ...... Suppose a sequence with 11 numbers: 1 2 3 4 3 2 1 8 9 9 8 suppose that after 7th numbers, the first 7 are a back-to-text sequence, the last four are also a return sequence, so we may try to start from the center of the Left return string, a clockwise, a counter-clockwise. After simulation, we will find that the answer is: enumeration tangent, the maximum length of the left-side echo sub-sequence plus the maximum length of the right-side echo sub-sequence.
#include <cstdio> #include <algorithm> using namespace std; const int maxn = 1000 + 10; int n, a[maxn], d[maxn][maxn]; void read(){ for(int i = 1; i <= n; i++) scanf("%d", &a[i]); } void solve(){ for(int i = 1; i <= n; i++) d[i][i] = 1; for(int len = 2; len <= n; len++) for(int i = 1; i <= n-len+1; i++){ int j = i + len - 1; if(a[i] == a[j]) d[i][j] = d[i+1][j-1] + 2; else d[i][j] = max(max(d[i+1][j], d[i][j-1]), d[i+1][j-1]); } int Max = -1; for(int i = 1; i <= n; i++) Max = max(Max, d[1][i] + d[i+1][n]); printf("%d\n", Max); } int main() { while(scanf("%d", &n) == 1 && n){ read(); solve(); } return 0; }