Description
You work in a machinery factory, your boss let you put a set of gear type sequence A1,A2,.., an take a few gear transmission ratio of 1:1, the boss asked you to take the least gear, can not change the original relative position of the gear, meet the conditions, that is, gear type combination is palindrome string. Input
Multiple sets of data, the first line has an integer t, which indicates that there is a T group of data. (t<=100)
The first line of each of the following data sets has an integer n, which indicates n gears (1<=n<=1000)
The next line has n integers a1,a2,..., an indicates the gear type (1<=ai<=10000) Output
Minimum number of gears taken from Sample Input
1
4
1 2 3 1
Sample Output
1
HINT
This is a template problem, read the question will know
and knock on the template.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int NUM[MAXN], DP[MAXN][MAXN];
int n;
int main () {
#ifdef LOCAL
freopen ("Input.txt", "R", stdin);
Freopen ("Output.txt", "w", stdout);
#endif
int T;
scanf ("%d", &t);
while (t--) {
scanf ("%d", &n);
for (int i=0; i<n; ++i)
scanf ("%d", num+i);
memset (DP, 0, sizeof (DP));
for (int i=n-1; i>=0;-I.) {
dp[i][i] = 1;
for (int j=i+1; j<n; ++j)
if (num[i] = = Num[j])
dp[i][j] = dp[i+1][j-1]+2;
else
dp[i][j] = max (dp[i][j-1], dp[i+1][j]);
}
printf ("%d\n", n-dp[0][n-1]);
}
return 0;
}