| 编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛
学习型 ASP/PHP/ASP.NET 主机 35元/年全能ASP/PHP/ASP.NET主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
赛孚耐:软件保护加密专家身份认证令牌USB KEY  
共有 317 人关注过本帖
标题:算法求助:1231231234 --> 11353 (加加号或者乘号)
收藏  订阅  推荐  打印 
HJin
Rank: 12Rank: 12Rank: 12
等级:贵宾
威望:27
帖子:401
积分:4134
注册:2007-6-9
算法求助:1231231234 --> 11353 (加加号或者乘号)

Problem statment:

You are given a squence of numbers. Put + and * at appropriate places so that the math expression equals the target number. For example,

1231231234 --> 11353

12*3+1+23*123*4 = 11353 // my algorithm shows this example has a unique soln

and two more examples:

"3456237490", 1185 -> "3*4*56+2+3*7+490" // my algorithm shows this example has 3 solns
"3456237490", 9191 -> "no solution"


第三次被问到同一题目了,在面试的时候。我有一个笨的办法:search over all 3^(n-1) possible combinations, where n is the length of the sequence, and use an expression stack to evalute。Obviously, my algorithm is O(3^n), or I have exponential time complexity.

We have around 3^(n-1)choices since we can put +, *, or nothing between two digits:
1+2
1*2
12

The question is: can we have an algorithm of polynomial complexity in time?

First I thought this can be done by using some dynamic programming or greedy algorithms. But I cannot show the optimal substructure or the greedy choice property. (These concepts come from Cormen's Introduction to Algorihtms, MIT press, 2nd ed.)

Do you have any idea?

[此贴子已经被作者于2007-6-9 21:27:56编辑过]

搜索更多相关主题的帖子: 乘号  加号  算法  example  
2007-6-9 12:01
wxj120bw
Rank: 1
等级:新手上路
帖子:15
积分:250
注册:2007-6-9

At present,I don't have any idea.But I advise you that you express your problem in Chinese next time.

2007-6-9 13:32
关于我们 | 广告合作 | 编程中国 | 清除Cookies | Archiver | WAP | TOP

编程中国 版权所有,并保留所有权利。鲁ICP备08000592号
Powered by Discuz, Processed in 0.054826 second(s), 9 queries.
Copyright©2004-2008, BCCN.NET, All Rights Reserved