import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = new String();
string = scanner.next();
char[] st = new char[string.length()];
st = string.toCharArray();
int[][] dp = new int[1001][1001];
int n = string.length();
for(int len = 1; len <= n; len ++) {
for(int l = 0; l + len - 1< n; l ++) {
int r = l + len - 1;
if(l == r) {
dp[l][r] = 1;
}
else {
dp[l][r] = Math.max(dp[l + 1][r],dp[l][r - 1]);
if(st[l] == st[r]) {
dp[l][r] = Math.max(dp[l][r], dp[l+1][r-1] + 2);
}
}
}
}
System.out.print( n - dp[0][n - 1]);
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for(int t = 0; t < T; t ++) {
int n, m;
m = scanner.nextInt();
n = scanner.nextInt();
int[][] dp = new int[m+1][n+1];
dp[0][0] = 1;
for(int i = 0; i <= m; i ++) {
for(int j = 1 ; j <= n; j ++) {
dp[i][j] = dp[i][j - 1];
if(i >= j)
dp[i][j] += dp[i-j][j];
}
}
System.out.println(dp[m][n]);
}
}
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[105][105];
int main()
{
int n, k;
cin >> n >> k;
memset(dp, -0x3f, sizeof(dp));
dp[0][0] = 0;
for(int i = 1; i <= n; i ++){
int w;
cin >> w;
for(int j = 0; j < k; j ++){
dp[i][j] = max(dp[i-1][j], dp[i-1][(j + k - w % k ) % k] + w);
}
}
cout << dp[n][0] << endl;
return 0;
}
|