help..问题求解

dhrbaobao 2006-11-19 02:01:57
这里有个题。做了半天了。提交上还是wrong answer。小弟初学c++。请指点
题目:Alphacode

Time Limit : 1 second Memory Limit : 32768 KB
Total Submit : 2036 Accepted Submit : 398


Problem
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
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

Output
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.


Sample input
25114
1111111111
3333333333
0


Sample output
6
89
1

我的程序:
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;


int stn(string s)//string to int
{
char c;
char *h=&c;
int sum,i,x;
x=1;
sum=0;
for (i=s.size()-1;i>=0;i--)
{
c=s[i];
sum+=atol(h)*x;
x*=10;
}
return sum;
}

void main()
{
long ss[10000],fl[10000],i,sum,si;
string h,hh;
sum=0;
cin>>h;
while (h!="0")
{
si=h.size();
//deal the 0..用a来代表10.b refer to 20;
//***************************new string*********************
hh="";

for (i=0;i<=(si-1);i++)
{
if (h[i+1]=='0')
{
if (h[i]=='1')
hh+='a';
else hh+='b';
i++;
}

else hh+=h[i];
}
h=hh;
//***************************new string*********************
//***************************do*********************
ss[1]=1;
if ((h[1]!='a')&&(h[1]!='b'))
{
if (stn(h.substr(0,2))<=26) ss[2]=2;
else ss[2]=1;
}
else ss[2]=1;


for (i=3;i<=h.size();i++)
{
if ((h[i-1]!='a')&&(h[i-1]!='b'))
{
if ((h[i-2]=='a')||(h[i-2]=='b'))
ss[i]=ss[i-1];
else
if ((stn(h.substr(i-2,2))<=26)) ss[i]=ss[i-1]+ss[i-2];
else ss[i]=ss[i-1];
}
else ss[i]=ss[i-1];
}
sum++;
fl[sum]=ss[h.size()];
cin>>h;
}
for (i=1;i<=sum;i++) cout<<fl[i]<<endl;
}


程序不是很规范。请大家看看是哪里的问题。谢谢。。
...全文
253 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssoju 2006-11-19
  • 打赏
  • 举报
回复

f(n) = f(n-1) + 2 (a[n] and a[n-1] 构成一个新字母 a[n-1]a[n]<=26) or f[n-1] + 1( a[n]与a[n-1]不构成一个新字母 a[n-1]a[n] >26)你的思路是用这个递归式,然后改为动态规划吗?
yanpeng54321 2006-11-19
  • 打赏
  • 举报
回复
太强了吧
这么高级
jixingzhong 2006-11-19
  • 打赏
  • 举报
回复
wrong answer 的原因是什么啊 ~~~

超时,还是程序处理不正确
dhrbaobao 2006-11-19
  • 打赏
  • 举报
回复
wrong answer是学校的测试系统测试的。题目算是用动态规划算得递归。但是不知道怎么错了
taodm 2006-11-19
  • 打赏
  • 举报
回复
不认识英文。题目翻译成中文才考虑是不是看看
ssoju 2006-11-19
  • 打赏
  • 举报
回复
这题有点小复杂。得想想看了。
Re: 《Linux 系统常用内置命令》  ---------------------------------------内容提要:     01. Linux内置命令概述;两栖命令 echo pwd kill 等等    02. 判断内置命令的方式;help -d [command] 能够正常回显    03. 内置命令在 shell 脚本中的基础应用介绍    04. 内置命令 alias 和 unalias 自定义别名和删除别名;临时生效与永久生效    05. 后台前台 出将入相 腾腾转 任务相关命令 bg fg jobs    06. SHELL 循环的处理方式 break continue exit    07. 常见 builtin 内置命令 eval exit export history    08. 内置命令 read 交互式赋值变量;type 判断命令类型    09. 内置命令 ulimit 修改系统资源使用限制;unset 清空变量  本人在教学和实战过程中发现,即便是有一定运维经验的人,可能已经能够搭建一定复杂度的Linux架构,但是在来来回回的具体操作中,还是体现出CLI(命令界面)功底不够扎实,甚至操作的非常‘拙’、处处露‘怯’。 对一个士兵来说,枪就是他的武器,对于一个程序员来说,各种library(工具库)就是他的武器;而对于Linux运维人员来说,无疑命令行工具CLI(命令界面)就是他们的武器;高手和小白之间的差距往往就体现在对于这些“武器”的掌握和熟练程度上。有时候一个参数就能够解决的事情,小白们可能要写一个复杂的Shell脚本才能搞定,这就是对CLI(命令界面)没有理解参悟透彻导致。 研磨每一个命令就是擦拭手中的作战武器,平时不保养不理解,等到作战的时候,一定不能够将手中的武器发挥到最好,所以我们要平心、静气和专注,甘坐冷板凳一段时间,才能练就一身非凡的内功! 本教程从实战出发,结合当下流行或最新的Linux(v6/7/8 版本)同时演示,将命令行结合到解决企业实战问题中来,体现出教学注重实战的务实精神,希望从事或未来从事运维的同学,能够认真仔细的学完Linux核心命令的整套课程。 本课程系列将逐步推出,看看我教学的进度和您学习的步伐,孰占鳌头! 注:关于教学环境搭建,可以参考本人其它课程系列,本教学中就不再赘述!  《参透 VMware 桌面级虚拟化》 《在虚拟机中安装模版机(包括应用软件等)》 《SecureCRT 连接 GNS3/Linux 的安全精密工具》
LINDO和LINGO数学建模软件使用文档 LINDO是一种专门用于求解数学规划问题的软件包。由于LINDO执行速度很快、易于方便输入、求解和分析数学规划问题。因此在数学、科研和工业界得到广泛应用。LINDO主要用于解线性规划、非线性规划、二次规划和整数规划等问题。也可以用于一些非线性和线性方程组的求解以及代数方程求根等。LINDO中包含了一种建模语言和许多常用的数学函数(包括大量概论函数),可供使用者建立规划问题时调用。 一般用LINDO(Linear Interactive and Discrete Optimizer)解决线性规划(LP?Linear Programming)。整数规划(IP?Integer Programming)问题。其中LINDO 6 .1 学生版至多可求解多达300个变量和150个约束的规划问题。其正式版(标准版)则可求解的变量和约束在1量级以上。 LINDO则用于求解非线性规划(NLP?NON?LINEAR PROGRAMMING)和二次规则(QP?QUARATIC PROGRAMING)其中LINGO 6.0学生版最多可版最多达300个变量和150个约束的规则问题,其标准版的求解能力亦再10^4量级以上。虽然LINDO和LINGO不能直接求解目标规划问题,但用序贯式算法可分解成一个个LINDO和LINGO能解决的规划问题。 要学好用这两个软件最好的办法就是学习他们自带的HELP文件。 下面拟举数例以说明这两个软件的最基本用法。(例子均选自张莹《运筹学基础》)

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧