题意
求一个序列中满足隔一位相等的子序列的最大长度。
思路
定义 表示结尾为第 倒数第二位为 位的子序列的最大长度。
则易得 。
Code
#include<iostream>
using namespace std;
int n, a[4001], tmp, ans;
int dp[4001][4001];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++)
{
tmp = 0;
for (int j = 0; j < i; j++)
{
dp[i][j] = dp[j][tmp] + 1;
if (a[i] == a[j]) tmp = j;
ans = max(ans, dp[i][j]);
}
}
cout << ans << endl;
return 0;
}