程序填空题:Decode Count
Suppose that a string of English letters is encoded into a string of numbers. To be more specific, `A`-`Z` are encoded into `0`-`25`. Since it is not a prefix code, the decoded result may not be unique. For example, `1213407` can be decoded as `BCBDEAH`, `MBDEAH`, `BCNEAH`, `BVDEAH` or `MNEAH`. Note that `07` is not `7`, hence cannot be decoded as `H`.
The function `DecodeCount` is supposed to return the number of different ways (modulo `BASE` to avoid overflow) we can decode `NumStr`, where `NumStr` is a string consisting of only the numbers `0`-`9`. Please complete the following program.
```c++
int DecodeCount( char NumStr[] )
{
int L, i;
int dp[MAXN];//dp[i] is the solution from NumStr[i] to the end
L = strlen(NumStr);
if (L==0) return 0;
if (L==1) return 1;
dp[L-1] = 1;
if (NumStr[L-2]!='1' && (NumStr[L-2]!='2' || NumStr[L-1]>'5'))
dp[L-2] = 1;
else dp[L-2] = 2;
for (i=L-3; i>=0; i--) {
if (NumStr[i]!='1' && (NumStr[i]!='2' || NumStr[i+1]>'5'))
dp[i] = @@[dp[i+1]](2);
else dp[i] = @@[dp[i+1] + dp[i+2]](2);
dp[i] %= BASE; //to avoid overflow
}
return @@[dp[0]](2);
}
```
答案:
第1空:dp[i+1]
第2空:dp[i+1] + dp[i+2]
第3空:dp[0]
The function `DecodeCount` is supposed to return the number of different ways (modulo `BASE` to avoid overflow) we can decode `NumStr`, where `NumStr` is a string consisting of only the numbers `0`-`9`. Please complete the following program.
```c++
int DecodeCount( char NumStr[] )
{
int L, i;
int dp[MAXN];//dp[i] is the solution from NumStr[i] to the end
L = strlen(NumStr);
if (L==0) return 0;
if (L==1) return 1;
dp[L-1] = 1;
if (NumStr[L-2]!='1' && (NumStr[L-2]!='2' || NumStr[L-1]>'5'))
dp[L-2] = 1;
else dp[L-2] = 2;
for (i=L-3; i>=0; i--) {
if (NumStr[i]!='1' && (NumStr[i]!='2' || NumStr[i+1]>'5'))
dp[i] = @@[dp[i+1]](2);
else dp[i] = @@[dp[i+1] + dp[i+2]](2);
dp[i] %= BASE; //to avoid overflow
}
return @@[dp[0]](2);
}
```
答案:
第1空:dp[i+1]
第2空:dp[i+1] + dp[i+2]
第3空:dp[0]