hdu 4632区间 dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632

用点容斥原理转移状态,

dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]

当 s[i] == s[j], 要加上 dp[i+1][j-1] + 1;

我就是想的太复杂,而这个题有卡时间,无限TLE,悲剧啊!

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>

#define maxn 1050
#define INF  0x3f3f3f
using namespace std;
const  int moder = 10007;

int dp[maxn][maxn];
char s[maxn];
int n;

int main()
{
    //if(freopen("input.txt","r",stdin)== NULL)  {printf("Error\n"); exit(0);}
    int T;
    cin>>T;
    for(int t=1;t<=T;t++){
        scanf("%s",s);
        n = strlen(s);    
        for(int i=0;i<n;i++)    dp[i][i] = 1;
        for(int i=0;i<n;i++)    dp[i][i-1] = 0;
        for(int i=n-2;i>=0;i--)
           for(int j=i+1;j<n;j++){
                 dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
               if(s[i] == s[j])   dp[i][j] += dp[i+1][j-1] + 1;
               dp[i][j] = (dp[i][j] + moder) % moder;  //没加moder,wa了好几次; 
              } 
        printf("Case %d: %d\n",t,dp[0][n-1]);
    }
}
View Code

 

posted @ 2013-08-02 17:22  等待最好的两个人  阅读(210)  评论(1编辑  收藏  举报