题目
In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence <A, B, D> is a subsequence of <A, B, C, D, E, F>.
(http://en.wikipedia.org/wiki/Subsequence)
Given a string S, your task is to find out how many different subsequence of S is palindrome. Note that for any two subsequence X = <S x1, S x2, …, S xk> and Y = <S y1, S y2, …, S yk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S xi = S yi. Also two subsequences with different length should be considered different.
Input
The first line contains only one integer T (T<=50), which is the number of test cases. Each test case contains a string S, the length of S is not greater than 1000 and only contains lowercase letters.
Output
For each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.
Sample Input
4
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems
Sample Output
Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960
题意
求字符串子串中回文串的数量%10007
思路
区间dp,i到j回文串的长度可以由i到j-1和i+1到j两段得到,但两段有重复的区间,减去i+1到j-1,当s[i]=s[j]需要特判,在之前的基础上加上dp[i+1][j-1]+1.
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<stack>
#include <queue>
using namespace std;
typedef long long ll;
const int inff=0x3f3f3f3f;
char s[1100];
int dp[1100][1100];
int main()
{
int t;
cin>>t;
int cs=1;
while(t--)
{
cin>>s;
memset(dp,0, sizeof(dp));
int l=strlen(s);
for(int i=0;i<l;i++)
dp[i][i]=1;
for(int len=1;len<l;len++)
{
for(int i=0;i+len<l;i++)
{
int j=i+len;
if(s[i]==s[j])
dp[i][j]=(dp[i+1][j]+dp[i][j-1]+1)%10007;
else
dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007;
}
}
cout<<"Case "<<cs<<": ";
cout<<dp[0][l-1]<<endl;
cs++;
}
return 0;
}