sicily 1001. Alphacode dp
2010-12-12 23:20
369 查看
看了动态规划,对dp有个大概理解,这题之前一直卡着,刚开始还用搜索做~
要注意对0的处理,开始把方程列错,WA数次
dp方程为 a
= a[n-1]+a[n-2],输入的字符串一次为X1X2X3…Xn…
当Xn = 0时,a
= a[n-2]
当Xn != 0时,
若X(n-1)与X(n)组成的数大于26, a
= a[n-1].
若X(n-1)与X(n)组成的数<=26,
x(n-1)不为0时, a
= a[n-1] + a[n-2]
x(n-1) = 0时, a
= a[n-1]
要注意对0的处理,开始把方程列错,WA数次
dp方程为 a
= a[n-1]+a[n-2],输入的字符串一次为X1X2X3…Xn…
当Xn = 0时,a
= a[n-2]
当Xn != 0时,
若X(n-1)与X(n)组成的数大于26, a
= a[n-1].
若X(n-1)与X(n)组成的数<=26,
x(n-1)不为0时, a
= a[n-1] + a[n-2]
x(n-1) = 0时, a
= a[n-1]
#include <iostream> #include <string> #define MAX 100000 using namespace std; int main() { string num; long long a[MAX]; int size; int n; while (cin >> num && num != "0") { size = num.length(); a[0] = 1; a[1] = 1; if (size == 1) { cout << "1" << endl; continue; } //对第二位处理 n = (num[0]-'0')*10 + num[1]- '0'; if (n <= 26 && num[1] != '0') a[1] = 2; for (int i = 2; i < size; i++) { if (num[i] == '0') a[i] = a[i-2]; else { a[i] = a[i-1]; n = (num[i-1]-'0')*10 + num[i]-'0'; if (n <= 26 && num[i-1] != '0') a[i] += a[i-2]; } } cout << a[size-1] << endl; } return 0; }
相关文章推荐
- sicily 1001. Alphacode (DP)
- Sicily 1001. Alphacode
- Sicily 1001. Alphacode
- sicily 1001. Alphacode
- Sicily 1001 Alphacode
- Sicily.1001. Alphacode
- Sicily 1001. Alphacode
- Sicily 1001. Alphacode
- Sicily 1001 Alphacode
- 动态规划实例:"Alphacode" From Sicily 1001
- [sicily] 1001. Alphacode
- Sicily.1001.Alphacode
- sicily-1001 alphacode
- Sicily.1001. Alphacode
- Sicily 1001.Alphacode | 动态规划
- sicily 1001. Alphacode
- Sicily 1001. Alphacode
- 【艾米莉娅】Sicily:1001. Alphacode 代码分享
- Sicily-1001. Alphacode
- [sicily online]1001. Alphacode