Alphacode 简单的动态规划问题。
来源:互联网 发布:电脑编程入门自学黑客 编辑:程序博客网 时间:2024/06/07 23:31
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.
样例输入
25114111111111133333333330
样例输出
689
这道题的实质就是分解一串字符,看有多少种组合(限制条件是数字在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;}
- Alphacode 简单的动态规划问题。
- pku2033 Alphacode (动态规划)
- Sicily 1001.Alphacode | 动态规划
- 动态规划实例:"Alphacode" From Sicily 1001
- POJ 2033|SPOJ ACODE|Alphacode|动态规划
- 简单的动态规划问题(帮助理解动态规划)
- 简单动态规划问题
- 一个简单的动态规划问题
- 从Alphacode看斐波那契数列与线性动态规划
- 动态规划----贪心的动态规划问题
- 动态规划法求解简单的(0/1)背包问题
- 数塔问题,简单的动态规划算法
- 删数问题最简单的动态规划
- OJ 7215 简单的整数划分问题__动态规划
- 简单的动态规划——装箱问题
- poj1221简单的动态规划
- HDU2084 简单的 动态规划
- 简单版的动态规划
- eclipse从安装到配置一条龙记录(下载、安装、外观配色字体、文件编码、代码提示、JRE与源码路径&Javadoc、tomcat等的配置)
- C++ string类的使用
- css中伪类after before用法
- 【帝国CMS】列表页调取副表字段演示
- JAVA自己面试遇到的问题
- Alphacode 简单的动态规划问题。
- 计算机视觉、机器学习相关领域论文和源代码大集合
- CountDownLatch详解
- 二进制中有多少个1
- 4(2)
- 数据结构中树的知识点
- 先别管那些算法了,从实战中开始大数据机器学习(一)
- 中国大学MOOC-陈越、何钦铭-数据结构-2017秋 03-树1 树的同构(25 point(s))
- 19.find — xargs的使用实例