hdu4632 Palindrome subsequence ——区间动态规划

link:http://acm.hdu.edu.cn/showproblem.php?pid=4632

refer to:

o(╯□╰)o……明明百度找的题解,然后后来就找不到我看的那份了,这位哥们对不住了……

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cmath>
 6 #include <cctype>
 7 #include <algorithm>
 8 #include <queue>
 9 #include <deque>
10 #include <queue>
11 #include <list>
12 #include <map>
13 #include <set>
14 #include <vector>
15 #include <utility>
16 #include <functional>
17 #include <fstream>
18 #include <iomanip>
19 #include <sstream>
20 #include <numeric>
21 #include <cassert>
22 #include <ctime>
23 #include <iterator>
24 const int INF = 0x3f3f3f3f;
25 const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
26 using namespace std;
27 char a[1111];
28 int dp[1111][1111];
29 const int MOD = 10007;
30 int main(void)
31 {
32     #ifndef ONLINE_JUDGE
33     freopen("in.txt", "r", stdin);
34     #endif // ONLINE_JUDGE
35     ios::sync_with_stdio(false);
36     int t; cin>>t;
37     for (int k = 1; k <= t; ++k)
38     {
39         cin>>a; int len = strlen(a);
40         memset(dp, 0, sizeof(dp));
41         for (int i = 0; i < len; ++i) for (int j = 0; j < len; ++j)
42             if (i==j) dp[i][j] = 1;
43         for (int i = 0; i < len; ++i)
44         {
45             for (int j = 0; i+j < len; ++j)
46             {
47                 if (a[i+j] == a[j])
48                 {
49                     dp[j][i+j] = dp[j][i+j-1] + dp[j+1][i+j] + 1;
50                 }
51                 else
52                 {
53                     dp[j][i+j] = dp[j+1][i+j] + dp[j][i+j-1] - dp[j+1][i+j-1];
54                 }
55                 dp[j][i+j] = (dp[j][i+j] + MOD)%MOD;
56             }
57         }
58         cout<< "Case "<<k<< ": "<<dp[0][len-1]<<endl;
59     }
60     return 0;
61 }
62 /*
63     模拟一下第二个样例 aaaaa 就懂了
64 */

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

posted on 2013-08-03 16:19  aries__liu  阅读(356)  评论(0编辑  收藏  举报