动态规划之UNIMODAL PALINDROMIC DECOMPOSITIONS
2015-08-03 19:59
381 查看
UNIMODAL PALINDROMIC DECOMPOSITIONS
Description
A sequence ofpositive integers is Palindromic if it reads the same forward and backward. Forexample:
23 11 15 1 37 37 1 15 11 23
1 1 2 3 4 7 7 10 7 7 4 3 2 1 1
A Palindromic sequence is Unimodal Palindromic if the values do not decrease upto the middle value and then (since the sequence is palindromic) do notincrease from the middle to the end For example, the first example sequenceabove is NOT Unimodal Palindromic
while the second example is.
A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of aninteger N, if the sum of the integers in the sequence is N. For example, all ofthe Unimodal Palindromic Decompositions of the first few integers are givenbelow:
1: (1)
2: (2), (1 1)
3: (3), (1 1 1)
4: (4), (1 2 1), (2 2), (1 1 1 1)
5: (5), (1 3 1), (1 1 1 1 1)
6: (6), (1 4 1), (2 2 2), (1 1 2 1 1), (3 3),
(1 2 2 1), ( 1 1 1 1 1 1)
7: (7), (1 5 1), (2 3 2), (1 1 3 1 1), (1 1 1 1 1 1 1)
8: (8), (1 6 1), (2 4 2), (1 1 4 1 1), (1 2 2 2 1),
(1 1 1 2 1 1 1), ( 4 4), (1 3 3 1), (2 2 2 2),
(1 1 2 2 1 1), (1 1 1 1 1 1 1 1)
Write a program, which computes the number of Unimodal PalindromicDecompositions of an integer.
Input
Input consists ofa sequence of positive integers, one per line ending with a 0 (zero) indicatingthe end.
Output
For each inputvalue except the last, the output is a line containing the input value followedby a space, then the number of Unimodal Palindromic Decompositions of the inputvalue. See the example on the next page.
Sample Input
2
3
4
5
6
7
8
10
23
24
131
213
92
0
Sample Output
2 2
3 2
4 4
5 3
6 7
7 5
8 11
10 17
23 104
24 199
131 5010688
213 1055852590
92 331143
题目意思解读:题意很简单,就是找单峰回路的总数目。
解题思路:这题是典型的动态规划问题。通过观察题目中给出的机组数字可以发现以下规律:
为了方便描述,我们对符号做一下说明,key[i][j]表示当n=i时回文最小数字为j的总数,value[i][j]表示当n=i时,回文的最小数字为j的总数。由此,我们可以很清晰的看出如下公式:key[i][j]=value[i][j]+value[i][j+1]+……+value[i][i]
显然对于上述公式,当i>2*j时value[i][j]=key[i-2*j][j]
当i=2*j或者是i=j时,value[i][j]=1
其他情况下value[i][j]=0;
由此得到的程序为:(由于程序很简单就不注释了)
#include<iostream>
using namespace std;
#define maxn 300
long long key[maxn][maxn];
int n;
void dp();
int main()
{
cin>>n;
while(n!=0)
{
dp();
cin>>n;
}
return0;
}
void dp()
{
for(inti=0;i<=n;i++)
{
for(intj=0;j<=n;j++)
{
key[i][j]=0;
}
}
key[1][1]=1;
for(inti=1;i<=n;i++)
{
for(intj=i;j>=1;j--)
{
longlong sum=0;
for(intk=j;k<=i;k++)
{
if(k==i)
sum+=1;
elseif(i<2*k)
sum+=0;
elseif(i==2*k)
sum+=1;
else
sum+=key[i-2*k][k];
}
key[i][j]=sum;
}
}
cout<<n<<""<<key
[1]<<endl;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解