http://acm.fafu.edu.cn/problem.php?id=1237
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
//fafu 1237 dp //题意:给一串数字,要从第一个跳到最后一个所能达到的 //最大能量为多少,相邻的数字不消耗能量,其他的看跳几个数字 //就消耗多少能量,若跳到相同的数字上,获得相应数字的能量 //同时也要消耗能量 //具体看代码,听说这题可以优化到O(10n),还没想到 #include <stdio.h> #include <string.h> #define N 1005 int dp[N]; char str[N]; int max(int a, int b) { return a > b ? a : b; } int main() { int n_case; scanf("%d", &n_case); for(int p = 1; p <= n_case; ++p) { scanf("%s", str); int len = strlen(str); for(int i = 0; i < len; ++i) { dp[i] = 0; for(int j = i-1; j >= 0; --j) { if(str[i] == str[j]) { //计算从j 到 i 是后所剩的 能量 int cost = dp[j] + str[j] - '0' - (i-j-1); if(dp[i] < cost) //更行i 的能量 dp[i] = cost; } else //若两个数字不相等,就一步一步(不用消耗能量)从j 走到 i dp[i] = max(dp[i], dp[j]); } } printf("Case %d: %d\n", p, dp[len-1]); } return 0; }