* 動態規劃(2) 編程題#1:UNIMODAL PALINDROMIC DECOMPOSITIONS(梯形數組 *R[N] 省空間,while (cin >> N){,long long型數組)

編程題#1:UNIMODAL PALINDROMIC DECOMPOSITIONS

來源: POJ (http://bailian.openjudge.cn/practice/1221/)

注意: 總時間限制: 1000ms 內存限制: 65536kB

描述
A sequence of positive integers is Palindromic if it reads the same forward and backward. For example:
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 up to the middle value and then (since the sequence is palindromic) do not increase from the middle to the end For example, the first example sequence above is NOT Unimodal Palindromic while the second example is.

A Unimodal Palindromic sequence is a Unimodal Palindromic Decomposition of an integer N, if the sum of the integers in the sequence is N. For example, all of the Unimodal Palindromic Decompositions of the first few integers are given below:
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 Palindromic Decompositions of an integer.

輸入
Input consists of a sequence of positive integers, one per line ending with a 0 (zero) indicating the end.

輸出
For each input value except the last, the output is a line containing the input value followed by a space, then the number of Unimodal Palindromic Decompositions of the input value. See the example on the next page.

樣例輸入

2
3
4
5
6
7
8
10
23
24
131
213
92
0

樣例輸出

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

提示
N < 250

解題思路參考:
http://blog.sina.com.cn/s/blog_7223fd910100x2bw.html

注:
求解最後的結果只需要枚舉一下回文序列中間的數字就可以了(遞推的終止條件!!!);偶數有兩個,奇數只有一個。
用long long型數據

程序解答:

#include<iostream>  
#include<cstring>  
using namespace std;

//全局變量 R[251][251],動態規劃的精髓!
long long R[251][251];   //記錄數字N的以不小於m開頭的單峯迴文串數

int main(){
    int N;
    memset(R, 0, sizeof(R));
    //cout << "當N最大取12時,以下依次輸出 i " << "j " << "i-2*j " << "k " << ": " << "R[i][j]" << endl;
    for (int i = 1; i <= 250; ++i){
        //cout << endl;
        R[i][i] = 1;          //數字n以n開頭則只有一種情況,如 6: (6);也是遞推的終止條件!!!
        if (i % 2 == 0)
            R[i][i / 2] = 1;  //偶數n以n/2開頭則也只有一種情況,如 6: (3 3);也是遞推的終止條件!!!
        for (int j = 1; i - 2 * j >= j; ++j){  //此處k要從j處開始循環!
            //此處i>=3時,j才能開始循環!
            for (int k = j; i - 2 * j >= k; ++k){
                R[i][j] += R[i - 2 * j][k];
                //cout << "i:" << i << " " << "j:" << j << " " << "i-2*j:" << i - 2 * j << " " << "k:" << k << " :: " << R[i][j] << "   ";
                //cout << i << " " << j << " " << i - 2 * j << " " << k << " : " << R[i][j] << "   ";
            }
            //cout << endl;
        }
    }

    while (cin >> N){
        if (N == 0)
            break;
        long long sum = 0;  // sum也得是 long long 型!
        for (int i = 1; i <= N; ++i)
            sum += R[N][i];
        cout << N << " " << sum << endl;
    }

    return 0;
}

其它解法參考:
http://blog.csdn.net/kindlucy/article/details/7434771



節省空間技巧(定義梯形數組 *R[N] 省空間):

long long *R[N]; 
for (int i = 0; i< N; i++){
    R[i] = new long long[i + 1];
    for (int j = 0; j <= i; j++){
        R[i][j] = 0;
    }
}



程序內部動態變化分析

當N最大取12時的內部變化舉例:
比如R[8][1]=R[6][1]=R[4][1]=R[2][1]=1, R[2][1]是遞推終止條件!!!
比如R[9][2]=R[5][4]=0, R[5][4]是遞推終止條件!!!
比如R[9][2]=R[5][5]=1, R[5][5]是遞推終止條件!!!

當N最大取12時,以下依次輸出 i j i-2*j k : R[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 12 2 8 5 : 3 12 2 8 6 : 3 12 2 8 7 : 3 12 2 8 8 : 4
12 3 6 3 : 1 12 3 6 4 : 1 12 3 6 5 : 1 12 3 6 6 : 2
12 4 4 4 : 1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章