您的位置:首页 > 编程语言 > C语言/C++

动态规划之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++ 动态规划