sicily 1001. Alphacode dp

        看了动态规划,对dp有个大概理解,这题之前一直卡着,刚开始还用搜索做~

       要注意对0的处理,开始把方程列错,WA数次

        dp方程为 a[n] = a[n-1]+a[n-2],输入的字符串一次为X1X2X3…Xn…

        当Xn = 0时,a[n] = a[n-2]

       当Xn != 0时,

               若X(n-1)与X(n)组成的数大于26, a[n] = a[n-1].

              若X(n-1)与X(n)组成的数<=26,

                      x(n-1)不为0时, a[n] = a[n-1] + a[n-2]

                      x(n-1) = 0时,  a[n] = 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;

}



你可能感兴趣的:(Alpha)