给定 t 个字符串,对于每个字符串 s,求出它的回文子序列的个数。
注意:不同的子序列,指的是对于两个子序列,只要有不同位置的字符,这两个子序列就算不用的子序列,即使它们的字符都相同。
例如,对于字符串 "aba",回文子序列有 "a", "a", "aa", "b", "aba" 共 5 个,其中两个 "a" 来自字符串的不同位置,算不同的子序列。
这位小盆友的代码:
#include<bits/stdc++.h>
using namespace std;
int NumOfPalindromeSubSequence(string str){
int len=str.length();
vector<vector<int> > dp(len,vector<int>(len));
for(int j=0;j<len;j++){
dp[j][j]=1;
for(int i=j-1;i>=0;i--){
dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1];
if(str[i]==str[j])dp[i][j]+=1+dp[i+1][j-1];
}
}
return dp[0][len-1];
}
int main(){
string str;
int num;
cin>>num;
for(int i=1;i<=num;i++){
cin>>str;
int p=NumOfPalindromeSubSequence(str);
cout<<"Case "<<i<<": "<<p%10007<<endl;
}
return 0;
}
有哪位热心的dalao帮忙调一下吗?我这个蒟蒻明显做不出来这个区间dp