【发布时间】:2015-12-30 09:33:39
【问题描述】:
以下是我对问题的实现:http://www.spoj.com/problems/ACODE/
#include <stdio.h>
#include <string.h>
int main() {
long long int dp[5010] = { 0 }, i, len, ans;
char str[5010];
scanf("%s", str);
while (str[0] != '0') {
len = strlen(str);
dp[0] =1;
for (i = 1; i < len; i++) {
ans = (str[i-1] * 10 + str[i]);
if (str[i] - '0')
dp[i] = str[i];
if (ans >= 10 && ans <= 26) {
if ((i - 2) < 0)
dp[i] += dp[0];
else
dp[i] += dp[i-2];
}
}
scanf("%s", str);
}
printf("%llu\n", dp[len-1]);
return 0;
}
当我在我的 IDE 中运行它时,它会被执行,但输出与预期完全不同。此外,当我在 Ideone 上运行它时,它会显示“超出时间限制”。请帮我找出我的错误。
【问题讨论】:
-
ans=(str[i-1]*10+str[i]);可能不会像你想的那样做;你在这里操作 ASCII 码。您还应该打印每个字符串的结果,即在循环内。请根据作业要求在结果后打印一个换行符。 -
@MOehm 你建议我应该在循环内写什么?
标签: c dynamic-programming