hdu4632 Palindrome subsequence ——区间动态规划

August 3, 2013
Hdu DP

link:http://acm.hdu.edu.cn/showproblem.php?pid=4632 refer to: o(╯□╰)o……明明百度找的题解,然后后来就找不到我看的那份了,这位哥们对不住了……


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <queue>
#include <deque>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <utility>
#include <functional>
#include <fstream>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
#include <ctime>
#include <iterator>
const int INF = 0x3f3f3f3f;
const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
using namespace std;
char a[1111];
int dp[1111][1111];
const int MOD = 10007;
int main(void)
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    int t; cin>>t;
    for (int k = 1; k <= t; ++k)
    {
        cin>>a; int len = strlen(a);
        memset(dp, 0, sizeof(dp));
        for (int i = 0; i < len; ++i) for (int j = 0; j < len; ++j)
            if (i==j) dp[i][j] = 1;
        for (int i = 0; i < len; ++i)
        {
            for (int j = 0; i+j < len; ++j)
            {
                if (a[i+j] == a[j])
                {
                    dp[j][i+j] = dp[j][i+j-1] + dp[j+1][i+j] + 1;
                }
                else
                {
                    dp[j][i+j] = dp[j+1][i+j] + dp[j][i+j-1] - dp[j+1][i+j-1];
                }
                dp[j][i+j] = (dp[j][i+j] + MOD)%MOD;
            }
        }
        cout<< "Case "<<k<< ": "<<dp[0][len-1]<<endl;
    }
    return 0;
}
/*
    模拟一下第二个样例 aaaaa 就懂了
*/

o(╯□╰)o永远感觉规划是一个很神奇的东西,比如这道。想明白了就感觉很神奇~

comments powered by Disqus