Alphacode 简单的动态规划问题。

问题:

1001. Alphacode

限制条件

时间限制: 1 秒, 内存限制: 32 兆

题目描述

Alice and Bob need to send secret messages to each other and are discussing ways to encode their messages: Alice: "Let's just use a very simple code: We'll assign `A' the code word 1, `B' will be 2, and so on down to `Z' being assigned 26." Bob: "That's a stupid code, Alice. Suppose I send you the word `BEAN' encoded as 25114. You could decode that in many different ways!" Alice: "Sure you could, but what words would you get? Other than `BEAN', you'd get `BEAAD', `YAAD', `YAN', `YKD' and `BEKD'. I think you would be able to figure out the correct decoding. And why would you send me the word `BEAN' anyway?" Bob: "OK, maybe that's a bad example, but I bet you that if you got a string of length 500 there would be tons of different decodings and with that many you would find at least two different ones that would make sense." Alice: "How many different decodings?" Bob: "Jillions!" For some reason, Alice is still unconvinced by Bob's argument, so she requires a program that will determine how many decodings there can be for a given string using her code.

输入格式

Input will consist of multiple input sets. Each set will consist of a single line of digits representing a valid encryption (for example, no line will begin with a 0). There will be no spaces between the digits. An input line of `0' will terminate the input and should not be processed

输出格式

For each input set, output the number of possible decodings for the input string. All answers will be within the range of a long variable.

样例输入

25114
1111111111
3333333333
0

样例输出

6
89
 
   
 
   
这道题的实质就是分解一串字符,看有多少种组合(限制条件是数字在1到26之间)
这道题如果用朴素的递归来做,会因递归次数过多而导致超时错误,所以采用动态规划,将子问题的最优解保存,将算法的时间复杂
降低到n平方;
根据算法导论的讲解,动态规划有两种实现方法,分别是从顶向下和自底向上,但其实质都是不变的,即将子问题的最优解保存下来
避免做过多的不必要的递归运算。
下面给出两种实现方法的代码:
自底向上:(某些oj可能不支持<bits/stdc++.h>)
#include <bits/stdc++.h>                //动态规划,自低向上实现。 
using namespace std;                     
                                      
int main()
{
	string str;
	while (cin >> str && str!="0"){
		int n = str.length();
		if(str[0]=='0'){
			cout << 0 <<endl;
			continue; 
		}
		int r[n+1];
		int q = 0;
		r[0] = 1;
		r[1] = 1;
		for (int i = 2; i <= n;i++){
			int x = str[i-2] - '0';
			int y = str[i-1] - '0';	
			int z = x*10 + y;
			if( z >= 11 && z<=26){
				q = r[i-2] + r[i-1];
			}
			else{
				q = r[i-1];
			}
			r[i] = q;
		}
		
		cout << r[n] << endl;
		
	return 0;
 } 
自顶向下:
#include <bits/stdc++.h>        //自顶向下(加入备忘机制的递归方法) 
using namespace std;


int couplenumber(int n, int r[],string str)
{
	string a = str;
	int q = 0;
	if (r[n] > 0) return r[n];
	else{
		int x = str[n-1]-'0';
		int y = str[n-2]-'0';
		int z = y*10 + x;
		if (str[n-1] == '0'){
			a.erase(n-2,2);
			q = couplenumber(n-2,r,a);
		}
		else if (str[n-2]=='0'){
			a.erase(n-1,1);
			q = couplenumber(n-1,r,a);
		}
		else if(z>=11 && z<=26){
			string b = a;
			a.erase(n-2,2);
			b.erase(n-1,1);
			q = couplenumber(n-2,r,a)+couplenumber(n-1,r,b);
		}
		else{
			a.erase(n-1,1);
			q=couplenumber(n-1,r,a);
		}
		r[n]=q;
	}
	return r[n];
}


int main()
{
	string str;
	while (cin >> str && str != "0"){
		int n = str.length();
		if ( str[0]=='0'){
			cout << 0 << endl;
		}
		int r[n+1];
		for (int i = 0;i<n+1;i++){
			r[i]=-1;
		}
		r[0]=1;
		r[1]=1;
		int outcome = couplenumber(n,r,str);
		cout << outcome << endl;
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_38742280/article/details/78303897
今日推荐