#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
typedef long long ll;
char s[100];
ll dp[100][100];
int main() {
int T;
scanf("%d", &T);
while (T--) {
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i < len; i++) {
dp[i][i] = 1;
}
for (int i = len - 2; i >= 0; i--) {
for (int j = i + 1; j < len; j++) {
if (s[i] == s[j]) {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] + 1;
} else {
dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1];
}
}
}
printf("%lld\n", dp[0][len - 1]);
}
return 0;
}
uva 10617 Again Palindrome (DP)
原文:http://blog.csdn.net/llx523113241/article/details/44946985